Hi
Consider the following program.
class T
{
int b, a;
T(int a, int b) : a(a), b(b) {}
};
g++ -Wall -c test.cc produces the following warning.
test.cc: In constructor `T::T(int, int)':
test.cc:3: Warnung: `T::a' will be initialized after
test.cc:3: Warnung: `int T::b'
test.cc:4: Warnung: when initialized here
Obviously, g++ likes the initialization order to be
the same as the declaration order of the corresponding
variables. Does anyone know whether/why it is critical
for them to have the same order?
Thanks for your help.
Regards,
Thomas 12 3990
"Thomas Heinz" <th*********@gmx.net> wrote in message
news:cc************@hades.rz.uni-saarland.de... Hi
Consider the following program.
class T { int b, a; T(int a, int b) : a(a), b(b) {} };
g++ -Wall -c test.cc produces the following warning.
test.cc: In constructor `T::T(int, int)': test.cc:3: Warnung: `T::a' will be initialized after test.cc:3: Warnung: `int T::b' test.cc:4: Warnung: when initialized here
Obviously, g++ likes the initialization order to be the same as the declaration order of the corresponding variables. Does anyone know whether/why it is critical for them to have the same order?
It isn't, but some people wrongly assume that the order of initalisation is
the same as the order in the initialisation list. So I would imagine the
reason for the warning is just to make sure people don't make that mistake.
john
Hi John
You wrote: It isn't, but some people wrongly assume that the order of initalisation is the same as the order in the initialisation list. So I would imagine the reason for the warning is just to make sure people don't make that mistake.
There is a misunderstanding. My question should rather be:
"Obviously, g++ likes the initialization order to be
the same as the declaration order of the corresponding
members." (not variables in the init. list).
However, I think one reason why the initialization order is
fixed by some means is that there can only be one destruction
order.
Regards,
Thomas
Thomas Heinz wrote: Hi John
You wrote:
It isn't, but some people wrongly assume that the order of initalisation is the same as the order in the initialisation list. So I would imagine the reason for the warning is just to make sure people don't make that mistake.
There is a misunderstanding. My question should rather be:
"Obviously, g++ likes the initialization order to be the same as the declaration order of the corresponding members." (not variables in the init. list).
However, I think one reason why the initialization order is fixed by some means is that there can only be one destruction order.
Yes, but that's not the essence of your statement, nor is it the essence
of John's reply. The reason for the warning is to _remind_ the user that
the order [of initialisation] is going to be the same as the declaration,
and not the order of initialisers in the list.
It's not that g++ "likes" it to be the same, it's more in line with g++
trying to keep the user from making the mistake. If g++ _liked_ it to be
specific, then it would probably issue an error instead of a warning.
Sorry to butt in on the conversation, just thought I'd add my $0.02...
V
Thomas Heinz wrote: class T { int b, a; T(int a, int b) : a(a), b(b) {} };
g++ -Wall -c test.cc produces the following warning.
test.cc: In constructor `T::T(int, int)': test.cc:3: Warnung: `T::a' will be initialized after test.cc:3: Warnung: `int T::b' test.cc:4: Warnung: when initialized here
Obviously, g++ likes the initialization order to be the same as the declaration order of the corresponding variables. Does anyone know whether/why it is critical for them to have the same order?
The definite initialization order is followed by g++, not just because
it likes, but because the C++ standard says so.
Here is an excerpt from the C++ standard for the same:
<C++STD>
12.6.2 - Initializing bases and members [class.base.init]
5.
Then, nonstatic data members shall be initialized in the order they were
declared in the class definition (again regardless of the order of the
mem-initializers).
</C++STD>
--
Nithyanandham M
SIEMENS, Bangalore, India.
(Please remove 'AtoZ' from my address when replying by mail)
"Thomas Heinz" <th*********@gmx.net> wrote in message
news:cc************@hades.rz.uni-saarland.de... Hi
Consider the following program.
class T { int b, a; T(int a, int b) : a(a), b(b) {} };
g++ -Wall -c test.cc produces the following warning.
test.cc: In constructor `T::T(int, int)': test.cc:3: Warnung: `T::a' will be initialized after test.cc:3: Warnung: `int T::b' test.cc:4: Warnung: when initialized here
Obviously, g++ likes the initialization order to be the same as the declaration order of the corresponding variables. Does anyone know whether/why it is critical for them to have the same order?
As pointed out elsewhere, the standard calls for initialization in
declaration order. This is to allow for use of one variable in initializing
another. Suppose b was always (3*a) more than the value provided for b
(fancy encryption!). Your code might be:
....
T(int a, int b) : a(a), b(a*3 + b) {}
....
Can you see that this won't work because b will be initialized first, using
an uninitialized a? That's why the warning was issued.
Changing the order in the declaration would allow this initialization list
to work properly.
--
Gary
"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:c5********************@comcast.com... "Thomas Heinz" <th*********@gmx.net> wrote in message news:cc************@hades.rz.uni-saarland.de...
<snip> class T { int b, a; T(int a, int b) : a(a), b(b) {} };
A side note (and New Topic):
While the parameter list in a constructor can use the same identifiers as
the class member names, it is probably a bad habit to get into since it will
bite you if used for methods. I recommend using some convention to identify
parameters as different from the class members, say
T(int Ia, int Ib) : a(Ia), b(Ib) {}
or
T(int newA, int newB) a(newA) b(newB) {}
etc.
--
Gary
Thomas Heinz <th*********@gmx.net> wrote in message news:<cc************@hades.rz.uni-saarland.de>... ... Obviously, g++ likes the initialization order to be the same as the declaration order of the corresponding variables.
It's not that g++ "likes" that. It is carved in standard
Thomas Heinz wrote: Consider the following program.
class T { int b, a; T(int a, int b) : a(a), b(b) {} };
g++ -Wall -c test.cc produces the following warning.
test.cc: In constructor `T::T(int, int)': test.cc:3: Warnung: `T::a' will be initialized after test.cc:3: Warnung: `int T::b' test.cc:4: Warnung: when initialized here
Obviously, g++ likes the initialization order to be the same as the declaration order of the corresponding variables.
Yes. That's what the language standard requires it do do.
Does anyone know whether/why it is critical for them to have the same order?
It is required by the standard. As for being "critical"... I'd say you
should ask yourself whether the initialization order is critical in your
particular case. And if it is, you should either carefully keep the
member declarations in proper order (which is not always possible) or
move certain initializations from the initializer list to the
constructor's body.
--
Best regards,
Andrey Tarasevich
Thomas Heinz <th*********@gmx.net> wrote in message news:<cc************@hades.rz.uni-saarland.de>... Hi
Consider the following program.
class T { int b, a; T(int a, int b) : a(a), b(b) {} };
g++ -Wall -c test.cc produces the following warning.
test.cc: In constructor `T::T(int, int)': test.cc:3: Warnung: `T::a' will be initialized after test.cc:3: Warnung: `int T::b' test.cc:4: Warnung: when initialized here
Obviously, g++ likes the initialization order to be the same as the declaration order of the corresponding variables. Does anyone know whether/why it is critical for them to have the same order?
Thanks for your help.
Regards,
Thomas
The initialization order followed is the declaration order,
and not the order used in the constructor. Otherwise, you could
write two different constructors that initialize the variables
in two different orders. This is usually not important and
you can ignore this warning almost always. However, in special
cases this can make a difference. g++ generates the warning in
case you expect a to be initialized before b.
-Arijit
Hi Gary
You wrote: ... T(int a, int b) : a(a), b(a*3 + b) {} ... Can you see that this won't work because b will be initialized first, using an uninitialized a? That's why the warning was issued. Changing the order in the declaration would allow this initialization list to work properly.
class T
{
int b, a;
T(int a, int b) : a(a), b(a*3 + b) {}
};
is equivalent to
class T
{
int b, a;
T(int _a, int _b) : b(_a*3 + _b), a(_a) {}
};
and not to
class T
{
int b, a;
T(int a, int b) : b(T::a*3 + T::b), a(T::a) {}
};
Hence, in your case b is initialized using the
constructur parameter a and not the member a.
Regards,
Thomas
On Thu, 08 Jul 2004 10:37:40 -0700, Gary Labowitz wrote: "Gary Labowitz" <gl*******@comcast.net> wrote in message news:c5********************@comcast.com... "Thomas Heinz" <th*********@gmx.net> wrote in message news:cc************@hades.rz.uni-saarland.de... <snip> > class T > { > int b, a; > T(int a, int b) : a(a), b(b) {} > };
A side note (and New Topic): While the parameter list in a constructor can use the same identifiers as the class member names, it is probably a bad habit to get into since it will bite you if used for methods.
Good advice!
It is probably more surprising and risky in the constructor. Consider
needing to move the initialization of a member into the body of the
constructor in the future for some reason. This time, not the member,
but the constructor argument would be assigned:
struct S
{
int i;
explicit S(int i)
:
i(1) // <-- initialize member
{
i = 2; // <-- assign to argument
}
};
#include <iostream>
int main()
{
S s(0);
std::cout << s.i << '\n';
}
I recommend using some convention to identify parameters as different from the class members, say T(int Ia, int Ib) : a(Ia), b(Ib) {} or T(int newA, int newB) a(newA) b(newB) {} etc.
One of the popular conventions is to append an underscore to the
member name:
class C
{
int value_;
/* ... */
};
I find this kind of name decoration very helpful especially in the
implementation files (.cpp, .cc, etc) when I am trying to figure out
where an object is defined. The appended underscore makes me see right
away that the object is a member.
Otherwise, I would need to search back higher in the scopes to see
where the object was introduced. Only after not finding it that way, I
would realize that it might be a member.
Ali
"Thomas Heinz" <th*********@gmx.net> wrote in message
news:cc*************@news.t-online.com... Hi Gary
You wrote: ... T(int a, int b) : a(a), b(a*3 + b) {} ... Can you see that this won't work because b will be initialized first,
using an uninitialized a? That's why the warning was issued. Changing the order in the declaration would allow this initialization
list to work properly.
class T { int b, a; T(int a, int b) : a(a), b(a*3 + b) {} };
is equivalent to
class T { int b, a; T(int _a, int _b) : b(_a*3 + _b), a(_a) {} };
and not to
class T { int b, a; T(int a, int b) : b(T::a*3 + T::b), a(T::a) {} };
Hey, right you are. Thanks. That's what I meant.
--
Gary This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sony Antony |
last post by:
I have the following simple program in Solaris Forte compiler 5.4
producing the warning. Though it produces the warning, it works fine
as expected.
This has been compiling fine without any...
|
by: Matthew Del Buono |
last post by:
Don't try to solve the problem. I've found a way -- around or fixing it. I'm
just curious as to whether this is Microsoft's problem in their compiler or
if there's a standard saying this is to be...
|
by: Hafeez |
last post by:
I am having real trouble compiling this code
http://www.cs.wisc.edu/~vganti/birchcode/codeHier/AttrProj.tgz
The attachment shows errors when compiled using the current version of
g++ in a...
|
by: junky_fellow |
last post by:
Consider the following piece of code:
struct junk {
int i_val;
int i_val1;
char c_val;
};
int main(void)
{
|
by: Bob |
last post by:
Hi,
The compiler gives Warning 96 Variable 'cmdSource' is used before it has
been assigned a value. A null reference exception could result at runtime.
Dim cmdSource as SQlClient.SQLDataReader...
|
by: Charles Sullivan |
last post by:
I have a program written in C under Linux (gcc) which a user has
ported to run under AT&T SysV R4.
He sent me a copy of his makelog which displays a large number
of compiler warnings similar to...
|
by: Charles Sullivan |
last post by:
I have a number of functions, e.g.:
int funct1( int arg1, int arg2, int arg3 );
int funct2( int arg1, int arg2, int arg3 );
int funct3( int arg1, int arg2, int arg3 );
that are called via...
|
by: zeppe |
last post by:
Hi all,
I've a problem. The code that follows creates a warning in both gcc and
visual c++. However, I think it's correct: basically, there is a
function that return an object of a derived...
|
by: gil |
last post by:
Hi,
I'm trying to find the best way to work with compiler warnings. I'd
like to remove *all* warnings from the code, and playing around with
the warning level, I've noticed that compiling with...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |