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!!
constructors called on members of a class
hi,
lets say i have a class Foo, and another class Bar. Bar has a constructor which receives an int. so i do:
Quote:Original post by Charles Hwang
Could you explain why this needs to be done?
Aggregating members or passing data to their constructors?
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!!!
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!!!
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.
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.
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)
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.
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.
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 =)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement