473,666 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Questions of C++ FAQ

In C++ FAQ 20.5, "...here are the mechanical details of why you need a
virtual destructor when someone says delete using a Base pointer that's
pointing at a Derived object. When you say delete p, and the class of p has
a virtual destructor, the destructor that gets invoked is the one associated
with the type of the object *p, not necessarily the one associated with the
type of the pointer. This is A Good Thing. In fact, violating that rule
makes your program undefined."

What's "the class of p", Base class?

What's "the type of the object *p", Base or Derived?

"the destructor that gets invoked is..."
Both destructors in Base AND Derived are invoked. Why only one is mentioned
here?

Thanks in advance!
Jul 22 '05 #1
8 1041
* ctick:
In C++ FAQ 20.5, "...here are the mechanical details of why you need a
virtual destructor when someone says delete using a Base pointer that's
pointing at a Derived object. When you say delete p, and the class of p has
a virtual destructor, the destructor that gets invoked is the one associated
with the type of the object *p, not necessarily the one associated with the
type of the pointer. This is A Good Thing. In fact, violating that rule
makes your program undefined."

What's "the class of p", Base class?
Yes.

What's "the type of the object *p", Base or Derived?
The same as above, Base.

The FAQ could need a little rewording here, yes.
"the destructor that gets invoked is..."
Both destructors in Base AND Derived are invoked. Why only one is mentioned
here?


Both are invoked if the one in Base is virtual.

Otherwise only the one in Base is invoked.

The reason only one is mentioned is because only the one that is
directly invoked by the delete expression is relevant. When the one in
Derived is directly invoked (as can happen when you have a Derived*
pointer) it doesn't matter whether the Base destructor is virtual or
not, because the Derived destructor knows that Derived is derived from
Base, and invokes the Base destructor automagically. When the one in
Base is the one directly invoked, as happens when the pointer is a Base*
and the destructor in Base is non-virtual, it doesn't know anything
about any Derived object needing destruction. So nothing more happens.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
* Alf P. Steinbach:
* ctick:
In C++ FAQ 20.5, "...here are the mechanical details of why you need a
virtual destructor when someone says delete using a Base pointer that's
pointing at a Derived object. When you say delete p, and the class of p has
a virtual destructor, the destructor that gets invoked is the one associated
with the type of the object *p, not necessarily the one associated with the
type of the pointer. This is A Good Thing. In fact, violating that rule
makes your program undefined."

What's "the class of p", Base class?
Yes.

What's "the type of the object *p", Base or Derived?


The same as above, Base.


Oops, I meant of course Derived. *p is the object that p points to.
Which in this case is a Derived object.

The FAQ could need a little rewording here, yes.


Absolutely! ;-)

"the destructor that gets invoked is..."
Both destructors in Base AND Derived are invoked. Why only one is mentioned
here?


Both are invoked if the one in Base is virtual.

Otherwise only the one in Base is invoked.

The reason only one is mentioned is because only the one that is
directly invoked by the delete expression is relevant. When the one in
Derived is directly invoked (as can happen when you have a Derived*
pointer) it doesn't matter whether the Base destructor is virtual or
not, because the Derived destructor knows that Derived is derived from
Base, and invokes the Base destructor automagically. When the one in
Base is the one directly invoked, as happens when the pointer is a Base*
and the destructor in Base is non-virtual, it doesn't know anything
about any Derived object needing destruction. So nothing more happens.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #3
ctick wrote:
In C++ FAQ 20.5, "...here are the mechanical details
of why you need a virtual destructor when someone says delete
using a Base pointer that's pointing at a Derived object.
When you say delete p, and the class of p has a virtual destructor,
the destructor that gets invoked
is the one associated with the type of the object *p,
not necessarily the one associated with the type of the pointer.
This is A Good Thing.
In fact, violating that rule makes your program undefined."
This is bad advice and typical of the muddled thinking
that passes for "object oriented programming" these days.
What's "the class of p", Base class?
Pointer p is a pointer to an object of the Base type.
What's "the type of the object *p", Base or Derived?
Reference *p is a reference to an object of the Base type.
What the author means is that
p was converted from a pointer to an object of the Derived type
to a pointer to an object of the Base type.
Invocation of a virtual function for the Base type
will actually invoke the corresponding function
defined for the Derived type.
"the destructor that gets invoked is..."
Both destructors in Base AND Derived are invoked.
Why only one is mentioned here?


The author was simply careless.
Evidently, the author believes that,
when programmers write

delete p;

they mean to delete an object of the Derived type
if p was actually converted from a pointer
to an object of a Derived type.

This is a *very* weak argument for a *virtual* destructor.
The Base class could have defined a virtual member function:

void Base::free(void ) const {
delete this;
}

to accompany a "virtual constructor" member function:

Base* Base::allocate( void) {
return new Base;
}

The problem here is that so-called "object oriented programmers"
are attempting to mimic Java programmers. But C++ is *not* Java.
Java has a built-in garbage collector to clean up dangling references
so that Java programmers needn't worry about memory leaks.
Jul 22 '05 #4
ctick posted:
In C++ FAQ 20.5, "...here are the mechanical details of why you need a
virtual destructor when someone says delete using a Base pointer that's
pointing at a Derived object. When you say delete p, and the class of p
has a virtual destructor, the destructor that gets invoked is the one
associated with the type of the object *p, not necessarily the one
associated with the type of the pointer. This is A Good Thing. In fact,
violating that rule makes your program undefined."

What's "the class of p", Base class?

What's "the type of the object *p", Base or Derived?

"the destructor that gets invoked is..."
Both destructors in Base AND Derived are invoked. Why only one is
mentioned here?

Thanks in advance!


Imagine the following:

When a mammal, dies it exhales.

A dog is a mammal. Therefore when it dies it exhales. But my dog also barks
too when it dies.

Here we go:

class Mammal
{
public:

~Mammal(void)
{
Exhale();
}
};

class Dog : public Mammal
{
public:

~Dog(void)
{
Bark();
}
};
int main(void)
{
Dog* Sparky = new Dog;

delete Sparky; //Here, it barks, then it dies

//Now imagine that this is a wildlife centre for mammals. They'd don't
//care whether it's a dog or cat or monkey or rhino... they just care
//that it's a mammal. Take the following:

Mammal* Sparky2 = new Dog;

delete Sparky2; //Here, it DOESN'T bark before it dies.

//If Mammal had had a virtual destructor, then Yes, it would have
//barked before it died!
}
Jul 22 '05 #5
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:cb******** **@nntp1.jpl.na sa.gov
ctick wrote:
In C++ FAQ 20.5, "...here are the mechanical details
of why you need a virtual destructor when someone says delete
using a Base pointer that's pointing at a Derived object.
When you say delete p, and the class of p has a virtual destructor,
the destructor that gets invoked
is the one associated with the type of the object *p,
not necessarily the one associated with the type of the pointer.
This is A Good Thing.
In fact, violating that rule makes your program undefined."


This is bad advice and typical of the muddled thinking
that passes for "object oriented programming" these days.

What's "the class of p", Base class?


Pointer p is a pointer to an object of the Base type.
What's "the type of the object *p", Base or Derived?


Reference *p is a reference to an object of the Base type.
What the author means is that
p was converted from a pointer to an object of the Derived type
to a pointer to an object of the Base type.
Invocation of a virtual function for the Base type
will actually invoke the corresponding function
defined for the Derived type.

"the destructor that gets invoked is..."
Both destructors in Base AND Derived are invoked.
Why only one is mentioned here?


The author was simply careless.
Evidently, the author believes that,
when programmers write

delete p;

they mean to delete an object of the Derived type
if p was actually converted from a pointer
to an object of a Derived type.

This is a *very* weak argument for a *virtual* destructor.
The Base class could have defined a virtual member function:

void Base::free(void ) const {
delete this;
}

to accompany a "virtual constructor" member function:

Base* Base::allocate( void) {
return new Base;
}

The problem here is that so-called "object oriented programmers"
are attempting to mimic Java programmers. But C++ is *not* Java.
Java has a built-in garbage collector to clean up dangling references
so that Java programmers needn't worry about memory leaks.

I wonder what language you are trying to program in or whether you read the
full text of this particular FAQ. The FAQ refers to a situation in which
Base pointers may point to Derived objects, presumably in order to achieve
polymorphic behaviour. The programmer calls delete on these Base pointers
without knowing --- and without wanting to know --- the kind of object that
is pointed to. Making the destructors virtual ensures that the correct
destructor(s) will be called. This is a simple and elegant procedure.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #6
John Carson wrote:
I wonder what language you are trying to program in
or whether you read the full text of this particular FAQ.
The FAQ refers to a situation
in which Base pointers may point to Derived objects,
presumably in order to achieve polymorphic behaviour.
The programmer calls delete on these Base pointers
without knowing --- and without wanting to know ---
the kind of object that is pointed to.
Making the destructors virtual ensures that
the correct destructor(s) will be called.
This is a simple and elegant procedure.


It is an elegant procedure in *Java*.
In C++, it is a souce of memory leaks.
Jul 22 '05 #7

"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:cb******** **@nntp1.jpl.na sa.gov...
John Carson wrote:
I wonder what language you are trying to program in
or whether you read the full text of this particular FAQ.
The FAQ refers to a situation
in which Base pointers may point to Derived objects,
presumably in order to achieve polymorphic behaviour.
The programmer calls delete on these Base pointers
without knowing --- and without wanting to know ---
the kind of object that is pointed to.
Making the destructors virtual ensures that
the correct destructor(s) will be called.
This is a simple and elegant procedure.


It is an elegant procedure in *Java*.
In C++, it is a souce of memory leaks.


How so?

john
Jul 22 '05 #8
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:cb******** **@nntp1.jpl.na sa.gov
John Carson wrote:
I wonder what language you are trying to program in
or whether you read the full text of this particular FAQ.
The FAQ refers to a situation
in which Base pointers may point to Derived objects,
presumably in order to achieve polymorphic behaviour.
The programmer calls delete on these Base pointers
without knowing --- and without wanting to know ---
the kind of object that is pointed to.
Making the destructors virtual ensures that
the correct destructor(s) will be called.
This is a simple and elegant procedure.


It is an elegant procedure in *Java*.
In C++, it is a souce of memory leaks.

If you are referring to the manual calling of delete and hence the
possibility that the programmer may forget to call it, then this can be
handled by the use of smart pointers. The destructor must still be virtual.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #9

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

Similar topics

0
4091
by: softwareengineer2006 | last post by:
All Interview Questions And Answers 10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP) I have listed over 10000 interview questions asked in interview/placement test papers for all companies between year 2000-2005 in my website http://www.geocities.com/allinterviewquestion/ So please have a look and make use of it.
0
4580
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
2
7203
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
4
2506
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for cross-posting. I am trying to build a "checklist", where a user can navigate to an ASP page on the intranet which shows a list of "questions" that the user can check off. I am trying to figure out how to do this so that it is scalable, but I am...
8
7972
by: Krypto | last post by:
Hi, I have used Python for a couple of projects last year and I found it extremely useful. I could write two middle size projects in 2-3 months (part time). Right now I am a bit rusty and trying to catch up again with Python. I am now appearing for Job Interviews these days and I am wondering if anybody of you appeared for a Python Interview. Can you please share the questions you were asked. That will be great help to me.
0
1490
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers7.html C# Interview Questions and Answers 6 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers-6.html C# Interview Questions and Answers 5...
1
1617
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers7.html C# Interview Questions and Answers 6 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers-6.html C# Interview Questions and Answers 5...
0
4491
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers7.html C# Interview Questions and Answers 6 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers-6.html C# Interview Questions and Answers 5...
0
3425
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions http://interviewdoor.com/technical/C-Interview-Questions.htm C# Interview Questions http://interviewdoor.com/technical/C-sharp-Interview-Questions.htm C++ Interview Questions http://interviewdoor.com/technical/C++-Interview-Questions.htm
0
2934
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions http://interviewdoor.com/technical/C-Interview-Questions.htm C# Interview Questions http://interviewdoor.com/technical/C-sharp-Interview-Questions.htm C++ Interview Questions http://interviewdoor.com/technical/C++-Interview-Questions.htm
0
8440
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8866
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8638
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7381
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6191
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4365
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.