Multiple Inheritance

A

ambar.shome

Hi,

See this code snippet....

class base1
{
public:
~base1(){cout<<"base1 destructor called"<<endl;}
};

class base2
{
public:
~base2(){cout<<"base2 destructor called"<<endl;}
};

class derived: public base1, public base2
{
public:
~derived(){cout<<"derived destructor called"<<endl;}
};

void main()
{
base2* b2 = new derived();
cout<<"Ambar in UNIX"<<endl;
delete b2;
}

When I executed this on a Solaris machine and it has given following
output:

"base2 destructor called"

but when i tried to execute it has given me run-time error.

at "delete b2".


Can anyone tell me the reason of this type of behaviour of C++?
 
V

Victor Bazarov

See this code snippet....

class base1
{
public:
~base1(){cout<<"base1 destructor called"<<endl;}
};

class base2
{
public:
~base2(){cout<<"base2 destructor called"<<endl;}
};

class derived: public base1, public base2
{
public:
~derived(){cout<<"derived destructor called"<<endl;}
};

void main()

There is no 'void main' in C++.
{
base2* b2 = new derived();
cout<<"Ambar in UNIX"<<endl;
delete b2;
}

When I executed this on a Solaris machine and it has given following
output:

"base2 destructor called"

but when i tried to execute it has given me run-time error.

at "delete b2".


Can anyone tell me the reason of this type of behaviour of C++?

The behaviour is undefined. Deleting an object of a derived class through
a pointer to any of its base classes _without_ a virtual destructor cause
undefined behaviour.

V
 
M

msalters

(e-mail address removed) schreef:
class base1
{
public:
~base1(){cout<<"base1 destructor called"<<endl;}
...

When you're dealing with public inheritance, you should almost
always use virtual destructors. MI doesn't change that.

HTH,
Michiel Salters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top