Sign in to follow this  
graveyard filla

constructors called on members of a class

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this