What is the order within class initialization code?

P

PengYu.UT

Hi,

I have the following code. I don't understand why "a" and "b" has the
same content, even their initialization codes are different.

My first conjecture was that
"a.show()" gave me "2 4"
but
"b.show()" gave me "2 0",
because in b when _y is initialized _x is still 0 if the initialization
is done from left to right.

Best wishes,
Peng



#include <iostream>

class A{
public:
A(int x) : _x(x),_y(_x * _x){}
void show(){
std::cout << _x << " " << _y << std::endl;
}
private:
int _x;
int _y;
};

class B{
public:
B(int x) : _y(_x * _x),_x(x){}
void show(){
std::cout << _x << " " << _y << std::endl;
}
private:
int _x;
int _y;
};

int main(int argc, char *argv[]) {
A a(2);
a.show();
B b(2);
b.show();
//a and b shows the same thing.
}
 
K

Karl Heinz Buchegger

Hi,

I have the following code. I don't understand why "a" and "b" has the
same content, even their initialization codes are different.

The initialization is done in the order in which data members are listed
in the class:
class A{ ....
private:
int _x;
int _y;
};

_x gets initialzed first, then _y
class B{ ....
private:
int _x;
int _y;
};

_x gets initialized first, then _y

The order in which you list the members in an intialization
list is *not* important to the order in which initializations
are done.
 
P

Peter Koch Larsen

Hi,

I have the following code. I don't understand why "a" and "b" has the
same content, even their initialization codes are different.

My first conjecture was that
"a.show()" gave me "2 4"
but
"b.show()" gave me "2 0",
because in b when _y is initialized _x is still 0 if the initialization
is done from left to right.

Best wishes,
Peng



#include <iostream>

class A{
public:
A(int x) : _x(x),_y(_x * _x){}
void show(){
std::cout << _x << " " << _y << std::endl;
}
private:
int _x;
int _y;
};

class B{
public:
B(int x) : _y(_x * _x),_x(x){}
void show(){
std::cout << _x << " " << _y << std::endl;
}
private:
int _x;
int _y;
};

int main(int argc, char *argv[]) {
A a(2);
a.show();
B b(2);
b.show();
//a and b shows the same thing.
}
They are constructed in the following order:
First baseclasses, from left to right.
Second members in order of their declaration.
It thus seems like your B-constructor is all right (_y should be initialised
before _x), and you seem to have a compiler problem.

/Peter
 
K

Karl Heinz Buchegger

Peter said:
class B{
public:
B(int x) : _y(_x * _x),_x(x){}
void show(){
std::cout << _x << " " << _y << std::endl;
}
private:
int _x;
int _y;
};

int main(int argc, char *argv[]) {
A a(2);
a.show();
B b(2);
b.show();
//a and b shows the same thing.
}
They are constructed in the following order:
First baseclasses, from left to right.
Second members in order of their declaration.
It thus seems like your B-constructor is all right (_y should be initialised
before _x)

How come?
The members are declared in the order _x, _y in B.
So the initialization is done in the order:
first _x, then _y
, and you seem to have a compiler problem.

Not at all. The compiler is right.
 
P

Peter Koch Larsen

Karl Heinz Buchegger said:
Peter said:
[snip]They are constructed in the following order:
First baseclasses, from left to right.
Second members in order of their declaration.
It thus seems like your B-constructor is all right (_y should be
initialised
before _x)

How come?

Ahh.. I was confused. I read the original post as if b.show() printed "2 0"
and not "2 4" as it did and indeed should.
The members are declared in the order _x, _y in B.
So the initialization is done in the order:
first _x, then _y


Not at all. The compiler is right.

Yes - of course.


/Peter
 
O

Old Wolf

class B{
public:
B(int x) : _y(_x * _x),_x(x){}
void show(){
std::cout << _x << " " << _y << std::endl;
}
private:
int _x;
int _y;
};

int main(int argc, char *argv[]) {
B b(2);
b.show();
}

"b.show()" gave me "2 0",
because in b when _y is initialized _x is still 0 if
the initialization is done from left to right.

_x is never 0, regardless of the initialization order,
because you never set it to 0.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
FloridaHan

Latest Threads

Top