c++


Constructor argument and member with the same name [duplicate]


This question already has an answer here:
Initializing member variables using the same name for constructor arguments as for the member variables allowed by the C++ standard?
2 answers
I'm curious if the following code is valid. Static analysis is giving an error on this constructor.
Summary: Member variable 'A' is initialized by itself.
Summary: Member variable 'B' is initialized by itself.
Summary: Member variable 'C' is initialized by itself.
class Foo
{
public:
Foo(int A, int B, int C);
private:
int A;
int B;
int C;
}
Foo::Foo(int A, int B, int C) :
A(A),
B(B),
C(C)
{}
I know that this is not good practice and should probably be changed, however I would like to know if the static analysis warning is a false positive and the member variables would be initialized correctly.
Yes, this is a false positive: constructor parameters take precedence over members when evaluating expressions for member initialization list; at the same time, constructor parameters do not participate in the resolution process of member names, so everything works exactly the way you expect.
Your point about this being a bad practice is perfectly valid as well.
This is pretty valid, because they're in different scopes. For the member initializer list A(A), the 1st A is in the scope of the class, then the data member A will be found; the 2nd A (which is putted in the parentheses) is in the scope of the constructor, then the parameter A will be found (and hide the data member with the same name).
Quotes from the standard,
$15.6.2/2 Initializing bases and members [class.base.init]:
In a mem-initializer-id an initial unqualified identifier is looked up in the scope of the constructor's class and, if not found in that scope, it is looked up in the scope containing the constructor's definition.
and $15.6.2/15 Initializing bases and members [class.base.init]:
Names in the expression-list or braced-init-list of a mem-initializer
are evaluated in the scope of the constructor for which the
mem-initializer is specified. [ Example:
class X {
int a;
int b;
int i;
int j;
public:
const int& r;
X(int i): r(a), b(i), i(i), j(this->i) { }
};
initializes X​::​r to refer to X​::​a, initializes X​::​b with the
value of the constructor parameter i, initializes X​::​i with the
value of the constructor parameter i, and initializes X​::​j with the
value of X​::​i; this takes place each time an object of class X is
created. — end example ] [ Note: Because the mem-initializer are
evaluated in the scope of the constructor, the this pointer can be
used in the expression-list of a mem-initializer to refer to the
object being initialized. — end note ]
They would be initialized correctly, as instructed in the initialization list.
Indeed, compiling the code, gives:
Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall main.cpp
main.cpp:6:9: warning: private field 'A' is not used [-Wunused-private-field]
int A;
^
main.cpp:7:9: warning: private field 'B' is not used [-Wunused-private-field]
int B;
^
main.cpp:8:9: warning: private field 'C' is not used [-Wunused-private-field]
int C;
^
3 warnings generated.
where these warnings, are of course, irrelevant.
Further reading: Initializing member variables using the same name for constructor arguments as for the member variables allowed by the C++ standard?
I know that this is not good practice.
It's not a bad one either. The trade off is that you do not introduce new names (such as the capital/lowercase thing one usually sees). I think it's a matter of opinion.

Related Links

Converting char* to str strange output
What is the difference between a modifiable rvalue and a const rvalue?
Why does “template argument deduction for class templates” not work on a plain struct?
C++ thread still `joinable()` after it finishes execution?
Pointer tho another Object
Openmp and reduction on std::vector?
gmock EXPECT_CALL does not invoke the method in tested class
binding error in l-value reference and r-value reference
C++ How would I use sort to sort this two dimensional array?
Shifting Letters in Console C++
error: prototype for 'void TNode::inititTree(TNode*)' does not match any in class 'TNode'
QList of pointers, append causes run time error
Compilation error: boost is_complex in if
Why is the cout function outputting random numbers?
Cannot find OpenCV 2 Delaunay triangulation functions in OpenCV 3 [duplicate]
Is the Visual C++ implementation of std::async using a thread pool legal

Categories

HOME
openerp
recaptcha
data-warehouse
mongoose
mono
set
bloomberg
bootstrap-datepicker
speech-recognition
unity5
visual-foxpro
directshow
autofixture
spf
css-modules
crystal-reports-xi
directive
slim
widget
pthreads
m3u8
codecvt
filenet-p8
pixel
prestashop-1.6
kafka-consumer-api
twitter-typeahead
ajaxform
swtbot
file-permissions
premailer
workspace
jqxwidgets
google-surveys
measurement
coupon
pdb-files
protobuf-c
spring-4
multiple-domains
javabeans
gmail-contextual-gadgets
regex-negation
gperftools
psftp
filestream
war
youtube-analytics
r6
grayscale
xilinx-edk
jes
cics
evosuite
fluentbootstrap
measurement-studio
mypy
android-datepicker
dnspython
opscenter
embedded-sql
nuodb
apple-search-ads
r.js
erwin
satellite
machinekey
voxel
kindle
rational-team-concert
ubuntu-15.10
rouge
ratchet
reducers
inventory
struts2-jquery-grid
gridsplitter
du
gcdasyncsocket
sql-server-2008-express
jaxb2-maven-plugin
networkcredentials
curator
httplib
sqloledb
componentkit
unassigned-variable
fast-esp
restriction
objectarx
kendo-validator
graphael
microsoft-search-server
stubs
rs485
amber-smalltalk
jsonexception
file-association
opengl-es-1.1
customer-support
objectiveflickr
iphone-sdk-3.0
flotr
windows-explorer
application-design
pvcs

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile