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! 8 1036
* 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?
* 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?
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.
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!
}
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:cb**********@nntp1.jpl.nasa.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)
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.
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:cb**********@nntp1.jpl.nasa.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
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:cb**********@nntp1.jpl.nasa.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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
by: connectrajesh |
last post by:
INTERVIEWINFO.NET
http://www.interviewinfo.net
FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES
NO ADVERTISEMENT
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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: 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,...
|
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...
|
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: 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,...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |