constructors called on members of a class

Started by
9 comments, last by rypyr 19 years, 9 months ago
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!!
FTA, my 2D futuristic action MMORPG
Advertisement
class Foo{public:    Foo();    Bar poopie;};Foo::Foo() : poopie(10){}


-me
Could you explain why this needs to be done?
Quote:Original post by Charles Hwang
Could you explain why this needs to be done?


Aggregating members or passing data to their constructors?
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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!!!
FTA, my 2D futuristic action MMORPG
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.
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)
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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 =)
FTA, my 2D futuristic action MMORPG

This topic is closed to new replies.

Advertisement