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

the old virtual destructor nut

Hi,

I recently did a test which required the user to note that a base
class had a non virtual destructor. Hence the dtor wouldn't be called
when the object was deleted.
The code is below (from memory) it's straightforward enough. My
question is this;

Does the spec say that the base classes dtor *wont* be called, or that
the behavior is undefined....The difference is subtle but all the same
its there. Am I correct in thinking that the behaviour is undefined?
According to Scott Meyers, it's undefined. I don't have a copy of the
spec but I'd like to get clarification.

thanks mulcho.

GrahamO
#include <iostream>

using namespace std;

class base
{
public:

base()
{
cout << "base()" << endl;
}

~base()
{
cout << "~base() " << endl;
}
};

class derived : public base
{
public:

derived()
{
cout << "derived()" << endl;
}

~derived()
{
cout << "~derived() " << endl;
}
};

derived* makeDerived()
{
return new derived();
};

base* makeBase()
{
return new base();
};

void cleanUp(base* b)
{
delete b;
}
int main(int argc, char** argv)
{

base* b = makeBase();
derived* d = makeDerived();

cleanUp(b);
cleanUp(d);
return 0;
}
Jul 22 '05 #1
2 1110
grahamo wrote:
The difference is subtle but all the same
its there. Am I correct in thinking that the behaviour is undefined?


You're correct. The difference is NOT subtle, though.
With my compiler, on my platform, the following code

class B1 { };
class B2 { }; // no virtual dtor!
class D: public B1, public B2 { };
int main()
{
D* dp = new D;
B2* b2p = dp;
delete b2p;
}

causes a segmentation fault.
(Note that multiple inheritance is involved.)

Max
Jul 22 '05 #2
Thanks a million for that Max. All I have to do now is let the guy
that interviewed me know that his test is wrong.. ! :) :) Better wait
until I actually get the job though:)
cheers mate

GrahamO

"Max M." <ed***@maxim.comm2000.it> wrote in message news:<ce**********@newsreader.mailgate.org>...
grahamo wrote:
The difference is subtle but all the same
its there. Am I correct in thinking that the behaviour is undefined?


You're correct. The difference is NOT subtle, though.
With my compiler, on my platform, the following code

class B1 { };
class B2 { }; // no virtual dtor!
class D: public B1, public B2 { };
int main()
{
D* dp = new D;
B2* b2p = dp;
delete b2p;
}

causes a segmentation fault.
(Note that multiple inheritance is involved.)

Max

Jul 22 '05 #3

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

Similar topics

11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
7
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the...
23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
4
by: Tony Johansson | last post by:
Hello Experts!! Assume I have a base class called animal. I want this class to be abstract so I make the destructor pure virtual by having this statement. virtual ~Animal() = 0; Destructor...
26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
7
by: sam | last post by:
Hi, See when i reading a sourcecode of a program, I read that the constructor is ordinary and after that the programmer has written virtual destructor for that constructor . Why we use the...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
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...

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.