Virtual c'tor
Suppos I have this code:
class A {
public:
virtual A ();
};
class B: public A {
public:
B ();
};
B obj;
For obj, will the A c'tor be called automatically within the call to B c'tor?
or do I need to call A c'tor explicitly.
Constructors are implicitly static. Hence, they can't be virtual.
Have a look at something like a "class factory" for constructing types based on information at runtime.
Have a look at something like a "class factory" for constructing types based on information at runtime.
Quote:Original post by The C modest god
For obj, will the A c'tor be called automatically within the call to B c'tor?
yes, always. you can't declare it virtual but child class constructors always get called. it's just the destructor that needs to be virtual.
-me
Quote:Original post by PalidineQuote:Original post by The C modest god
For obj, will the A c'tor be called automatically within the call to B c'tor?
yes, always. you can't declare it virtual but child class constructors always get called. it's just the destructor that needs to be virtual.
-me
You mean parent c'tor will be called?
I call B(the child) c'tor and A(the parent) c'tor will be automatically called?
> You mean parent c'tor will be called?
> I call B(the child) c'tor and A(the parent) c'tor will
> be automatically called?
Yes.
> I call B(the child) c'tor and A(the parent) c'tor will
> be automatically called?
Yes.
It would be easier and faster just to put print statements in the base and derived classes to figure out this behavior, FYI.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement