if it is virtual a dtor will not be called? |
If you are calling a
non-virtual function through a pointer... the function of whatever class the
pointer's type is gets called. IE, if you call it with an A pointer, it will call the A function:
1 2 3 4 5
|
// assuming A has no virtual members
A* ptr = new B;
ptr->func(); // <- calls A::func... NOT B::func... because we have an A pointer
delete ptr; // <- calls A::~A... NOT B::~B... because we have an A pointer
| |
If you are calling a
virtual function through a pointer... the function of whatever class the object
actually is gets called. IE, if you have an A pointer that points to a B object... the B function gets called:
1 2 3 4 5
|
// assuming A has all virtual members
A* ptr = new B;
ptr->func(); // <- calls B::func because we are pointing to a B
delete ptr; // <- calls B::~B because we are pointing to a B
| |
and secondly on my example |
In your example, A's dtor is not virtual, so you could potentially have a problem if you delete an A pointer. B's dtor being virtual does not really help you unless you are deriving other classes from B.
, i think that the object B dtor will be ended because A is composed of B and not because of the virtual destructor , am i right? |
I'm not sure I'm understanding this question.
B's dtor will call A's dtor because B derives from A. This will happen regardless of whether or not the dtors are virtual.
A's dtor will never call B's dtor.