473,407 Members | 2,359 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,407 software developers and data experts.

delete by base class pointer?

class Base{};
class Obj : public Base{};

std::vector<Base*> vpBase;
Obj o = new Obj();
vpBase.push_back(&o);

// Can I safely do this: ?
delete vpBase[0];

Thank you,
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

Jul 22 '05 #1
3 1992

"Gernot Frisch" <Me@Privacy.net> skrev i en meddelelse
news:30*************@uni-berlin.de...
class Base{};
class Obj : public Base{};

std::vector<Base*> vpBase;
Obj o = new Obj();
vpBase.push_back(&o);

// Can I safely do this: ?
delete vpBase[0];

Thank you,
--
-Gernot
int main(int argc, char** argv) {printf ("%silto%c%cf%cgl%ssic%ccom%c",
"ma", 58, 'g', 64, "ba", 46, 10);}

No - undefined. But for most implementations this will turn out okay. Still
it is a silly example - there's not much going on here.

/Peter
Jul 22 '05 #2
Gernot Frisch wrote:

class Base{};
class Obj : public Base{};

std::vector<Base*> vpBase;
Obj o = new Obj();
vpBase.push_back(&o);

// Can I safely do this: ?
delete vpBase[0];


No. The destructor in the base class has
to be virtual.

class Base
{
public:
virtual ~Base() {}
};

Now you are safe.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3

"Karl Heinz Buchegger" <kb******@gascad.at> schrieb im Newsbeitrag
news:41***************@gascad.at...
Gernot Frisch wrote:

class Base{};
class Obj : public Base{};

std::vector<Base*> vpBase;
Obj o = new Obj();
vpBase.push_back(&o);

// Can I safely do this: ?
delete vpBase[0];


No. The destructor in the base class has
to be virtual.

class Base
{
public:
virtual ~Base() {}
};

Now you are safe.


Bingo. Now I completele understand 'virtual'. Thank you very much.
Jul 22 '05 #4

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

Similar topics

2
by: Ian McBride | last post by:
(was: delete() confusion) I have a class with multiple base classes. One of these base classes (base1) has its own new/delete operators and nothing else. Another base class (base 2) has a...
3
by: Dave | last post by:
Hello all, In the code below, I see the following output: base::operator new(size_t, int) base::base() base::~base() base::operator delete(void *) In the case of an exception being thrown...
3
by: Zycor | last post by:
Anybody have good knowledge of delete that can answer this simple question? I have a base class that contains pointers to its own type, those pointers will contain derived classes (acting...
10
by: MaxMax | last post by:
int *p; p = new int; void *q = (void*)p; delete q; Is this valid if p is guaranteed to be a POD? (don't ask why... there is a reason I need this. Clearly I will create p in function, use p...
4
by: lothar.behrens | last post by:
Hi, I have problems to delare a delete operator in a class and use it to check for valid pointer. Using release() with an additional validation routine from a separate malloc library avoids...
3
by: Nindi | last post by:
On comp.lang.c++.moderated http://groups.google.co.uk/group/comp.lang.c++.moderated/browse_thread/thread/8250715711da7760?hl=en the following question was posted ...
1
by: Gonçalo Rodrigues | last post by:
Hi all, I am a little confused about the delete operator, so I have a question. Suppose we have something like class Base { public: void* operator new(std::size_t size); void operator...
12
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.