473,395 Members | 1,631 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,395 software developers and data experts.

undefined behaviour ??

Consider the following code. Does it produce undefined behaviour
because destructor of A is not virtual ?

#include<iostream>
using namespace std;
class A
{
public:
A()
{
std::cout<<"In A's Constructor"<<std::endl;
}
~A()
{
std::cout<<"In A's Desstructor"<<std::endl;
}
};
class B:public A
{
public:
B()
{
std::cout<<"In B's Constructor"<<std::endl;
}
virtual ~B()
{
std::cout<<"In B's Desstructor"<<std::endl;
}
};
class C:public B
{
public:
C()
{
std::cout<<"In C's Constructor"<<std::endl;
}
~C()
{
std::cout<<"In C's Desstructor"<<std::endl;
}

};

int main()
{
A *b = new C();
delete b;
return 0;
}

Dec 20 '05 #1
5 1166
dragoncoder wrote:
Consider the following code. Does it produce undefined behaviour
because destructor of A is not virtual ?

#include<iostream>
using namespace std;
class A
{
~A()
{
std::cout<<"In A's Desstructor"<<std::endl;
}
};
class B:public A
{
virtual ~B()
{
std::cout<<"In B's Desstructor"<<std::endl;
}
};
class C:public B
{
public:
~C()
{
std::cout<<"In C's Desstructor"<<std::endl;
}

};

int main()
{
A *b = new C();
delete b;
return 0;
}


Yes, because you use a pointer to A.

Best regards,

Tom

Dec 20 '05 #2
Thomas Tutone wrote:
Yes, because you use a pointer to A.


Or, more accurately, because you delete a pointer to A.

Best regards,

Tom

Dec 20 '05 #3
dragoncoder wrote:
Consider the following code. Does it produce undefined behaviour
because destructor of A is not virtual ?
Yes.

[I've edited the code by snipping unnecessary functionality]
class A
{
public:
~A()
{
}
};
class B:public A
{
public:
virtual ~B()
{
}
};
class C:public B
{
public:
~C()
{
}
};

int main()
{
A *b = new C();
delete b;
return 0;
}


V
Dec 20 '05 #4
On 20 Dec 2005 06:44:04 -0800, "dragoncoder" <pk******@gmail.com>
wrote:
Consider the following code. Does it produce undefined behaviour
because destructor of A is not virtual ?


Yes.

--
Bob Hairgrove
No**********@Home.com
Dec 20 '05 #5
dragoncoder wrote:
Consider the following code. Does it produce undefined behaviour
because destructor of A is not virtual ?


Yes, exactly !

Have a look at :
http://www.codersource.net/cpp_virtual_destructors.html

Or type "virtual destructor c++" in google, and you will get an answer !

Regards,
Pierre-Andre Galmes
Dec 20 '05 #6

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

Similar topics

6
by: Simon Bailey | last post by:
In the following code at the end of the program z = 20 & y = 99. void doit(const int* x) { int* nonconst; nonconst = const_cast<int*>(x); *nonconst = 99; } int main(int argc, char* argv)
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
8
by: Joona I Palaste | last post by:
We all know that this: void *p; if (p=malloc(1)) { free(p); p; } causes undefined behaviour if malloc() succeeds. But what about this?
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
23
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64...
12
by: RoSsIaCrIiLoIA | last post by:
On Mon, 07 Feb 2005 21:28:30 GMT, Keith Thompson <kst-u@mib.org> wrote: >"Romeo Colacitti" <wwromeo@gmail.com> writes: >> Chris Torek wrote: >>> In article <4205BD5C.6DC8@mindspring.com> >>>...
26
by: Frederick Gotham | last post by:
I have a general idea of the different kinds of behaviour described by the C Standard, such as: (1) Well-defined behaviour: int a = 2, b = 3; int c = a + b; (Jist: The code will work...
12
by: Franz Hose | last post by:
the following program, when compiled with gcc and '-std=c99', gcc says test.c:6: error: jump into scope of identifier with variably modified type that is, it does not even compile. ...
10
by: subramanian100in | last post by:
Consider the following code: #include <iostream> #include <cstdlib> using namespace std; int main() { const double& ref = 100;
33
by: coolguyaroundyou | last post by:
Will the following statement invoke undefined behavior : a^=b,b^=a,a^=b ; given that a and b are of int-type ?? Be cautious, I have not written a^=b^=a^=b ; which, of course, is undefined....
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
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.