473,387 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

deleting multible inherited objects using a base class pointer

Why does program crash if object is deleted using base class pointer
that is not the first base class?

Please look exaple below.
#include <iostream>
class A
{
public:
virtual void printA(){std::cout << "A"<<std::endl;}
};

class B
{
public:
virtual void printB(){std::cout << "B"<<std::endl;}
};

class AB:public A, public B
{
};

void foo(A *p){delete p;}
void bar(B *p){delete p;}
int main()
{
AB *p = new AB();
foo(p);
AB *p2 = new AB();
bar(p2); // Crash
return 0;
}

Is there a way around this. I find it quite limiting that the order of
inheritance matters to ability to call foo but not bar
Best Regards,
Jussi

Jul 22 '05 #1
1 1289
On 14 Jul 2004 12:54:02 -0700, Softari <ju********@yahoo.com> wrote:
Why does program crash if object is deleted using base class pointer
that is not the first base class?

Please look exaple below.
#include <iostream>
class A
{
public:
virtual void printA(){std::cout << "A"<<std::endl;}
};

class B
{
public:
virtual void printB(){std::cout << "B"<<std::endl;}
};

class AB:public A, public B
{
};

void foo(A *p){delete p;}
void bar(B *p){delete p;}
int main()
{
AB *p = new AB();
foo(p);
AB *p2 = new AB();
bar(p2); // Crash
return 0;
}

Is there a way around this. I find it quite limiting that the order of
inheritance matters to ability to call foo but not bar
Best Regards,
Jussi


Because you don't have a virtual destructor. It is illegal to delete any
derived object though a base class pointer unless you have a virtual
destructor. First base class, second base class makes no difference, it's
illegal always.
class A
{
public:
virtual ~A() {}
virtual void printA(){std::cout << "A"<<std::endl;}
};

class B
{
public:
virtual ~B() {}
virtual void printB(){std::cout << "B"<<std::endl;}
};

Now it should work.

john
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Hanzo | last post by:
I'm iterating over a vector of base class pointers and deleting those which meet a certain criteria...i'm using pretty text-book code for the particular delete/erasure (it's straight out of Myers'...
9
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
2
by: Josh Mcfarlane | last post by:
I'm doing recomposition of objects from binary streams, and the best way for me to write them out is to write base class data first, forward to inherited classes, pointer class values, etc. Now,...
4
by: Mike Cooper | last post by:
There is something about inherited classes I evidently don't know... I wrote the following class: Class Class1 inherits System.Windows.Forms.DataGridTextBoxColumn End Class There is...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
0
by: porter_wss | last post by:
I am a bit confused on serializing objects and inherited objects. What I am trying to accomplish is creating a base object and an inherited object with additional properties. The sender of these...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
15
by: shuisheng | last post by:
Dear All, Assume I have a class named Obj. class Obj { }; And a class named Shape which is derived from Obj. class Shape: public Obj
21
by: George Exarchakos | last post by:
Hi everyone, I'd like your help... Can we have a std::list<BASEwhere BASE be the base class of a class hierarchy? I want to add to this list objects that are inherited from BASE class but not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.