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

Virtual destructors and the C++ standard.

I was recently surprised about how a chunk of code compiled and
executed, which lead me to wonder what would be "correct" from a C++
standards perspective. (I don't need help to arrive at sensible code,
this is for academic interest only...)

--
#include <iostream>
using namespace std;
class A {
public:
A() { cerr << "CA"; }
virtual ~A() =0; };
class B : public A {
public:
B(bool a) { if (a) throw a; }
~B() { cerr << "DA"; } };
int main(int c,char *v[])
{
try { B b(c==1); } catch(bool x) { cerr << "catch" << endl; }
return 0;
}
--
I'm interested to know:
* While this obviously compiles, is it complete - i.e. should it link?
* Should the fact that A has a pure virtual destructor influence
whether or not B's destructor is called in the context of exception
'a'?
* Have either of the above two questions different answers if one
looks from the perspective different C++ standards vintages?

Jun 10 '07 #1
2 1233
gg***********@shic.co.uk wrote:
I was recently surprised about how a chunk of code compiled and
executed, which lead me to wonder what would be "correct" from a C++
standards perspective. (I don't need help to arrive at sensible code,
this is for academic interest only...)

--
#include <iostream>
using namespace std;
class A {
public:
A() { cerr << "CA"; }
virtual ~A() =0; };
class B : public A {
public:
B(bool a) { if (a) throw a; }
~B() { cerr << "DA"; } };
int main(int c,char *v[])
{
try { B b(c==1); } catch(bool x) { cerr << "catch" << endl; }
return 0;
}
--
I'm interested to know:
* While this obviously compiles, is it complete - i.e. should it link?
* Should the fact that A has a pure virtual destructor influence
whether or not B's destructor is called in the context of exception
'a'?
* Have either of the above two questions different answers if one
looks from the perspective different C++ standards vintages?
According to my understanding it should not link. After class B's
destructor executes, it calls class A's destructor (see 12.4.6).
Because you've declared a destructor in class A, the implicit destructor
is not created for you (see 12.4.3). You did not define A's destructor
anywhere, so when you try to link, the call to it in B's destructor will
be unresolved.

Making a member function pure virtual, by the way, does NOT mean that
you cannot provide an implementation (see 10.3.8). It only imposes the
requirement that child classes must override the member function, and
that no instances of the class containing the pure virtual member
function may be created.

The solution? Provide a definition for A's destructor. This cannot be
done in a declaration (see note in 10.4.2), so add the following line
somewhere before main:
A::~A() {}

--
Alan Johnson
Jun 10 '07 #2
On Jun 10, 7:03 am, ggroups_st...@shic.co.uk wrote:
I was recently surprised about how a chunk of code compiled and
executed, which lead me to wonder what would be "correct" from a C++
standards perspective. (I don't need help to arrive at sensible code,
this is for academic interest only...)
--
#include <iostream>
using namespace std;
class A {
public:
A() { cerr << "CA"; }
virtual ~A() =0; };
class B : public A {
public:
B(bool a) { if (a) throw a; }
~B() { cerr << "DA"; } };
int main(int c,char *v[])
{
try { B b(c==1); } catch(bool x) { cerr << "catch" << endl; }
return 0;}
--
I'm interested to know:
* While this obviously compiles, is it complete - i.e. should it link?
It's undefined behavior, so technically, we can't say. In
practice, I can't imagine a system where it would link.
* Should the fact that A has a pure virtual destructor influence
whether or not B's destructor is called in the context of exception
'a'?
No. Pure virtual has no influence on anything here. By the
time we get into B's constructor, A has been fully constructed,
so its destructor must be called.

Typically, the compiler will not detect that the code after the
declaration of b is unreachable, nor that in fact, the complete
program will never call the destructor of B, and will generate a
call to A::~A in the destructor of B, which on most systems will
be sufficient to make the link fail unless there is a definition
somewhere.
* Have either of the above two questions different answers if one
looks from the perspective different C++ standards vintages?
No.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 10 '07 #3

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

Similar topics

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: Giancarlo Niccolai | last post by:
Hello all. I have peeked through the FAQ and all relevant links, and also through Stroustrup book, but I have not been able to find an answer, so I have to post here as a last resort. It...
11
by: Bonj | last post by:
Hello, Can anyone help me with these fairly simple questions. 1) What is the point in virtual destructors - I've heard it's a good thing for base-classes, but what are the advantages and...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
10
by: amparikh | last post by:
Ok, my question is not about Virtual destructors and why, but more on the significance. Generally we have a virtual destructor in the base class ( and inadvertently in the derived class) so that...
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...
9
by: desktop | last post by:
On this page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html Shape specify the virtual function: virtual double Intersect( const Shape& s) = 0; then the derived class Circle...
8
by: lmfmaw | last post by:
Hi all, I've hit the wall with this "fairly" simple problem. As you can see in the code below, my destructors don't do their job as they are supposed to (I left them empty for this example). I'm...
6
by: Pallav singh | last post by:
Hi All How Does compiler implement virtual destructor ??? as we all know if base class destructor is virtual..........then while wriiting statemnt like this Base * b = new Derived( );...
3
by: GAURAV AGRAWAL | last post by:
Hi Guys, Can someone please explain me why this is happening #include<iostream> using namespace std; class a { public: int a1; // If I remove this it'll work fine
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: 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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.