• Advertisement
Sign in to follow this  

constructors called on members of a class

This topic is 4978 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, lets say i have a class Foo, and another class Bar. Bar has a constructor which receives an int. so i do:

class Foo
{
   public:
   
    Bar some_bar(10);
};
error !!! why? i know its probably bad design, and i could just use pointers, or even better, a container, but why cant i do this in the first place? thanks for any help!!

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Charles Hwang
Could you explain why this needs to be done?


Aggregating members or passing data to their constructors?

Share this post


Link to post
Share on other sites
hmmmmmmmmm.......

so you have to default-ly construct the member, then, in the initiailzation list, call the real constructor ? and what did you mean by "Aggregating members"

thanks again!!!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you dont make assignments in the class definition. You would get the same error ( I think) if you tried to do something like:
int something = 5;

I don't think your supposed to have constants of any kind in the class def, though you can get around it by using enum. The way you put in constants is to use an initializer list, shown above by someone else.

Share this post


Link to post
Share on other sites
You don't default construct them, they aren't constructed until the enclosing class destructor is ran, and then they run in the order the aggregate (child) members were declared with the parameters from the initialization list (or default ctor calls if no params were given)

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
you dont make assignments in the class definition. You would get the same error ( I think) if you tried to do something like:
int something = 5;


the only thing you can initialize in the class definition is static const integral data.

Share this post


Link to post
Share on other sites
Quote:
Original post by graveyard filla
so you have to default-ly construct the member, then, in the initiailzation list, call the real constructor?


If you provide an initialization in the initalization list, the member doesn't get default constructed. The constructor with the appropriate parameter list (here, a single int) gets directly called.

Share this post


Link to post
Share on other sites
Quote:
Original post by DigitalDelusion
You don't default construct them, they aren't constructed until the enclosing class destructor is ran, and then they run in the order the aggregate (child) members were declared with the parameters from the initialization list (or default ctor calls if no params were given)


did you mean "enclosing class CONstructor is ran"?

so, its never default constructed? the Foo constructor will be called BEFORE the Bar constructor, and when the Foo constructor is called, the initialzation list will call the Bar constructor? so if somethings in the init. list, its never default constructed? thanks again!!

EDIT: posted after fruny.. i think i get it now =)

Share this post


Link to post
Share on other sites
FYI, I consider this a "broken" feature of C++ :( To my mind it should be feasible. It's just something that Java-heads have to live with ;)

Regards,
Jeff

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement