1 Replies - 446 Views - Last Post: 19 June 2011 - 07:38 AM Rate Topic: -----

#1 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

virtual destructor & vtable of classes

Posted 19 June 2011 - 07:19 AM

Hi guys ,

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.

Is This A Good Question/Topic? 0
  • +

Replies To: virtual destructor & vtable of classes

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 844
  • View blog
  • Posts: 2,334
  • Joined: 20-August 07

Re: virtual destructor & vtable of classes

Posted 19 June 2011 - 07:38 AM

If you do not specify a destructor for a class, the compiler will provide one for you; this is true irrespective of whether it derives from another class or not. objects will be destroyed from the top-downwards (i.e. the most-derived destructor will be called first, then the base destructor last)

virtual-ness is inherited, so anything which is flagged 'virtual' in a base class will also be virtual in derived classes too.


As far as I know, there's nothing in the standard which specifies how Vtables must be implemented, so I suspect the answer will depend on your implementation.

This post has been edited by Bench: 19 June 2011 - 07:44 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1