When is the contructor called for a class thats been declared as a member of another class if you dont implicitly call the contructor? Is it called at all?
When is the contructor called for a class thats been declared as a member of another class if you dont implicitly call the contructor? Is it called at all?
-------------------
"Exception"
be more specific please!
i didn't get much of that u got an example maybe
Try it out, this didn't take two minutes to type in:
This prints out the following on my system:Code:class Member { public: Member() { cout << "Member class constructor called." << endl; }; }; class Base { public: Member m_Member; Base() { cout << "Base class constructor called." << endl; }; }; int main() { Base MyBase; return 0; }
Member class constructor called.
Base class constructor called.
Looks like the Member class gets constructed first. Think about it a little. If the constructor for the Base class did stuff to the m_Member variable, itself a class, and that m_Member wasn't constructed yet, what would happen? So, the construction of any objects belonging to a class has to occur before the class itself.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
now take it easy again from the begining. u call that constructers
but i dont see any constructers in your code!
Those are the constructors.Code:Member() { cout << "Member class constructor called." << endl; }; Base() { cout << "Base class constructor called." << endl; };
maybe the compiler sees the constructor
correct me again as usual if i'm wrong Dual-Catfish but
shouldn't a constructor always have a value as argument
...yes i believe so
No, think of it as a function, if it needs an argument, give it one. If not, then don't.shouldn't a constructor always have a value as argument
u cant think that way
and it does need an argument
otherwise it would'nt be a constructor
>now take it easy again from the begining. u call that
>constructers but i dont see any constructers in your code!
That code relies on the default constructors that do not need to be defined, they're supplied by the compiler for the simple duty of creating objects. For further functionality you would need to define a specialized constructor.
-Prelude
My best code is written with the delete key.
For this particular instance, yes, you can. But you're correct, it's not right to think of constructors as functions.u cant think that way
As prelude said, the code is relying on default constructors (What! how can that be? Constructors have to have arguments!)and it does need an argument
otherwise it would'nt be a constructor
Oh well, this is way off topic... if you want to discuss it more PM me.
That was my problem...stupidity. I was wondering about it, because I know you can explicity call the constructor...so I was thinking, why would it call it for you if sometimes you had to call it? And the answer, as you guys have awesomely pointed out, is that it only calls the default constructor, and you need to call explicitly if you want another constructor called. Braine freeze on my part...StonedCoder left his mark on me. Anyway, am I right to awesome that call a constructor supresses the default constructor from being called?
-------------------
"Exception"