Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > constructor initialization list

Reply
Thread Tools

constructor initialization list

 
 
Makis Papapanagiotou
Guest
Posts: n/a
 
      11-03-2004
Hello all,

There is a strange case where a class Test is composed from three objects of
other classes. i.e.

class Test
{ public:
Test();
private:
Point x;
Cat y;
Dog z;
};

where the classes Point, Cat, Dog are

class Point
{ public:
Point();
Point(Point& x, Dog& y);
private:
};

class Cat
{ public:
Cat();
Cat(Point& x, Dog& y);
private:
};

class Dog
{ public:
Dog();
Dog(Point& x, Cat& y);
private:
};

If we were calling the default constructors they is no problem, since as
STROUSTRUP is wrtiing in his book they are called in the order they are
defined in the class the main class (Test).

My big problem is why the following is working? i.e when the other
constructors are called.

Test::Test()(), y(x,z), z(x,y)

since the creation of object "y" is related with the creation of object "z".

Can any body give an explanation of this strange behaviour?

Thanks in advance,


 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      11-03-2004

> Can any body give an explanation of this strange behaviour?



The objects are contructed in the order in which they are, in the
Initialization List.

For objects that *aren't* listed in the initialization list, they are
constructed *before* all other member objects. So, given the following:


#include <string>

class Blah
{
public:
std::string a;
std::string b;
std::string c;
std::string d;
std::string e;
std::string f;

Blah() : c("Monkey!"), e(), a(), f("Ape!");
};


The order of in which the member objects are consructed in the above is as
follows:

b
d
c
e
a
f


-JKop


 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      11-03-2004

> Test::Test()(), y(x,z), z(x,y)



Opps! I missed the point with my last post.


The above code is ill-formed.


-JKop
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      11-03-2004
Makis Papapanagiotou wrote:
>
> Hello all,
>
> There is a strange case where a class Test is composed from three objects of
> other classes. i.e.
>
> class Test
> { public:
> Test();
> private:
> Point x;
> Cat y;
> Dog z;
> };
>
> where the classes Point, Cat, Dog are
>
> class Point
> { public:
> Point();
> Point(Point& x, Dog& y);
> private:
> };
>
> class Cat
> { public:
> Cat();
> Cat(Point& x, Dog& y);
> private:
> };
>
> class Dog
> { public:
> Dog();
> Dog(Point& x, Cat& y);
> private:
> };
>
> If we were calling the default constructors they is no problem, since as
> STROUSTRUP is wrtiing in his book they are called in the order they are
> defined in the class the main class (Test).
>
> My big problem is why the following is working?


Define 'working'

> i.e when the other
> constructors are called.


It is still the same order:
x gets constructed first
y gets constructed next
z gets constructed last

>
> Test::Test()(), y(x,z), z(x,y)


The order in which constructors of member objects are called
is entirely defined by the order in which they are listed
in the class declaration. Here, in the initializer list
you simply specify *which* constructor to use for the members,
not the order in which they are called.

--
Karl Heinz Buchegger

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      11-03-2004
JKop wrote:
>
> > Can any body give an explanation of this strange behaviour?

>
> The objects are contructed in the order in which they are, in the
> Initialization List.


You might want to reread on this.
The order is defined entirely by the order the members are listed
in the class declaration. The initialzation list just selects which
constructor to use, but does not change the order in which members
get initialized.


--
Karl Heinz Buchegger

 
Reply With Quote
 
Makis Papapanagiotou
Guest
Posts: n/a
 
      11-03-2004
Hello,

Sorry but this is not covering my question since in your example the objects
are not related to each other.

In my case they are

Test:: x( ), y(x,z), z(x,y)

So the object "y" in order to be constructed needs the object "z", but "z"
in order to be constructed needs "y" first.
Even though they are constructed.

Hence how this is done? Because it is done and it is not an error !!!


Thanks,

"JKop" <> wrote in message
news:Xn5id.40762$...
>
> > Can any body give an explanation of this strange behaviour?

>
>
> The objects are contructed in the order in which they are, in the
> Initialization List.
>
> For objects that *aren't* listed in the initialization list, they are
> constructed *before* all other member objects. So, given the following:
>
>
> #include <string>
>
> class Blah
> {
> public:
> std::string a;
> std::string b;
> std::string c;
> std::string d;
> std::string e;
> std::string f;
>
> Blah() : c("Monkey!"), e(), a(), f("Ape!");
> };
>
>
> The order of in which the member objects are consructed in the above is as
> follows:
>
> b
> d
> c
> e
> a
> f
>
>
> -JKop
>
>



 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      11-03-2004
Karl Heinz Buchegger posted:

> JKop wrote:
>>
>> > Can any body give an explanation of this strange behaviour?

>>
>> The objects are contructed in the order in which they are, in the
>> Initialization List.

>
> You might want to reread on this.
> The order is defined entirely by the order the members are listed
> in the class declaration. The initialzation list just selects which
> constructor to use, but does not change the order in which members
> get initialized.



I'm wrong!


I wasn't exactly sure so I went off and tested it, but then I realized
that my "test" would have produced the same results either way.

So... in summation:

Objects are constructed in the order in which they appear in the class
declaration.

(I think what had me confused is where you have multiple base classes
and you use the initialization list to specify the order in which the
base classes' constructors are called.)


-JKop
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      11-03-2004
Makis Papapanagiotou wrote:
>
> Hello,
>
> Sorry but this is not covering my question since in your example the objects
> are not related to each other.
>
> In my case they are
>
> Test:: x( ), y(x,z), z(x,y)
>
> So the object "y" in order to be constructed needs the object "z", but "z"
> in order to be constructed needs "y" first.
> Even though they are constructed.
>
> Hence how this is done? Because it is done and it is not an error !!


It all depends on what the Cat or Dog constructor do with
the passed reference.

When the Cat constructor is called and passed the Dog object,
the Dog object isn't yet fully constructed, but the memory for
that object has already been reserved. So for the Cat object it
is safe to eg. store a pointer to the Dog object or create a reference
to it. You, the programmer, know that at exactly this memory location
eventually there will be a fully constructed Dog object, when the
whole initialization is completed.


--
Karl Heinz Buchegger

 
Reply With Quote
 
Makis Papapanagiotou
Guest
Posts: n/a
 
      11-03-2004
Hello,

Yes it is still in the same order:

x gets constructed first
y gets constructed next
z gets constructed last

At least thats what we see if we put a "cout" stetement in the
constructors...

Thanks,

"Karl Heinz Buchegger" <> wrote in message
news:...
> Makis Papapanagiotou wrote:
> >
> > Hello all,
> >
> > There is a strange case where a class Test is composed from three

objects of
> > other classes. i.e.
> >
> > class Test
> > { public:
> > Test();
> > private:
> > Point x;
> > Cat y;
> > Dog z;
> > };
> >
> > where the classes Point, Cat, Dog are
> >
> > class Point
> > { public:
> > Point();
> > Point(Point& x, Dog& y);
> > private:
> > };
> >
> > class Cat
> > { public:
> > Cat();
> > Cat(Point& x, Dog& y);
> > private:
> > };
> >
> > class Dog
> > { public:
> > Dog();
> > Dog(Point& x, Cat& y);
> > private:
> > };
> >
> > If we were calling the default constructors they is no problem, since as
> > STROUSTRUP is wrtiing in his book they are called in the order they are
> > defined in the class the main class (Test).
> >
> > My big problem is why the following is working?

>
> Define 'working'
>
> > i.e when the other
> > constructors are called.

>
> It is still the same order:
> x gets constructed first
> y gets constructed next
> z gets constructed last
>
> >
> > Test::Test()(), y(x,z), z(x,y)

>
> The order in which constructors of member objects are called
> is entirely defined by the order in which they are listed
> in the class declaration. Here, in the initializer list
> you simply specify *which* constructor to use for the members,
> not the order in which they are called.
>
> --
> Karl Heinz Buchegger
>



 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      11-03-2004
JKop posted:

>
>> Test::Test()(), y(x,z), z(x,y)

>
>
> Opps! I missed the point with my last post.
>
>
> The above code is ill-formed.
>
>
> -JKop



Again I'm wrong!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help on constructor/constructor initialization list with inheritanceusing templates balaji C++ 3 08-10-2011 12:06 PM
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
array initialization in initialization list. toton C++ 5 09-28-2006 05:13 PM
Initialization of non-integral type in initialization list anongroupaccount@googlemail.com C++ 6 12-11-2005 09:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57