Given the following code :
#include <iostream>
using namespace std;
class A
{
public:
A () {cout << "A1" <<endl;}
A(const A& a) {cout << "A2" <<endl; }
virtual ~A() {cout << "A3" << endl; }
void f() {cout << "A4" << endl; }
virtual void g() {h(); cout << "A5" << endl; }
virtual void h() {cout << "A6" << endl; }
};
class D
{
public:
D () {cout << "D1" <<endl;}
~D() {cout << "D2" <<endl; }
};
class B : public A
{
public:
D d;
B() {cout << "B1" <<endl;}
B(const B& B)/> {cout << "B2" <<endl; }
~B() {cout << "B3" << endl; }
void f() {cout << "B4" << endl; }
void h() {cout << "B5" << endl; }
virtual void i() {cout << "B6" << endl; }
};
class C : public B
{
public:
B b;
A a;
C() {cout << "C1" <<endl;}
virtual ~C() {cout << "C2" << endl; }
void f() {cout << "C3" << endl; }
virtual void h() {cout << "C4" << endl; }
void i() {cout << "C5" << endl; }
B j() {cout << "C6" << endl; return b;}
};
int main ()
{
A *ptrAtoB = new B();
B *ptrBtoC = new C();
cout << endl;
ptrAtoB->h();
cout << endl;
ptrBtoC->h() ;
cout << endl;
ptrAtoB->f();
cout << endl;
ptrBtoC->f();
cout << endl;
ptrAtoB->g();
cout << endl;
delete ptrBtoC;
delete ptrAtoB;
return 1;
}
1. As you can see , the base class "Class A" has a virtual destructor.Does it mean that every class
has in its Vtable , the destructor of A (i.e. ~A()) or its own destructor ?
2. What about the virtual methods of the upper levels ? if I have a virtual function at the Base class
and the derived class also has a virtual statement for that specific method , what would I see in the vtable ?
Here are my answers , I'd appreciate if you could tell me where I'm wrong :
A's Vtable : A::g() A::h() A::~A()
B's Vtable: A::g() B::h() B::~B()
C's Vtable: A::g() A::h() C::~C()
D has nothing , since it has no "new"
I'd appreciate any help ,Ron
10x.

New Topic/Question
Reply




MultiQuote



|