Click here to Skip to main content
15,918,808 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Text Length Pin
Chris Losinger4-Jul-06 14:30
professionalChris Losinger4-Jul-06 14:30 
GeneralRe: Text Length Pin
locoone4-Jul-06 14:32
locoone4-Jul-06 14:32 
GeneralRe: Text Length Pin
Chris Losinger4-Jul-06 15:34
professionalChris Losinger4-Jul-06 15:34 
QuestionSyntax related question [modified] Pin
Jay034-Jul-06 11:37
Jay034-Jul-06 11:37 
AnswerRe: Syntax related question Pin
Jörgen Sigvardsson4-Jul-06 11:47
Jörgen Sigvardsson4-Jul-06 11:47 
Questionvirtual destructors [modified] Pin
Jay034-Jul-06 10:59
Jay034-Jul-06 10:59 
AnswerRe: virtual destructors Pin
Joe Woodbury4-Jul-06 11:13
professionalJoe Woodbury4-Jul-06 11:13 
AnswerRe: virtual destructors Pin
Jörgen Sigvardsson4-Jul-06 12:02
Jörgen Sigvardsson4-Jul-06 12:02 
class A {
public:
   A() { }
   ~A() { }
};
 
class B : public A {
public:
   B() { }
   ~B() { }
};
 
B* b = new B();
delete b; // calls B::~B() and then A::~A() automatically
A* a = new B();
delete a; // calls A::~A() only, leaving stuff initialized by B::B() uninitialized


When a C++ compiler sees a method call, it first checks to see if the method is virtual or not. If it is not virtual, it will use the object variable's static type information, to determine what method to call. If the variable is of type B*, it will first look in B's definition, and fallback on A if no matching method could be found.

If the method is virtual, the compiler will generate code which calls the method furthest down in the inheritance tree.

The same idea applies to destructors. If you call delete on b, as above, the compiler will first check if the destructor is virtual in the class hierarchy. In this case, it's not. Therefore it will inspect B's definition, as the variable is of type B*. It'll find the destructor, and call it. After that, the compiler will generate code which calls all parent classes' destructors. In the case of the variable a, the compiler will, as earlier, see that the destructor is not virtual, and will therefore look into A's definition for the destructor (a is of type A*). The compiler will then generate code which calls A::~A(), and nothing more. You can easily understand that this is a problem, if B::B() did stuff like new ....

Two analogies: virtual destructors are like doubly linked lists; you can always find the end of the list from any node in the list - meaning that the compiler will always call the destructor furthest down the inheritance tree, and then work its way back to the root. Non-virtual destructors are like singly linked lists; you can not find the end of the list from all nodes in the list - meaning that the compiler can't call the destructors which comes "after" the class in question.

The golden rule: virtual methods are virtual, if and only if, the first declaration is declared as virtual.


--
Transmitido en Martian en SAP
GeneralRe: virtual destructors Pin
Jay034-Jul-06 12:39
Jay034-Jul-06 12:39 
GeneralRe: virtual destructors Pin
Jörgen Sigvardsson4-Jul-06 13:39
Jörgen Sigvardsson4-Jul-06 13:39 
GeneralRe: virtual destructors [modified] Pin
Jay034-Jul-06 14:25
Jay034-Jul-06 14:25 
GeneralRe: virtual destructors [modified*2] Pin
Jörgen Sigvardsson4-Jul-06 14:27
Jörgen Sigvardsson4-Jul-06 14:27 
GeneralRe: virtual destructors [modified*2] Pin
Jay034-Jul-06 14:50
Jay034-Jul-06 14:50 
GeneralRe: virtual destructors Pin
Jörgen Sigvardsson4-Jul-06 14:53
Jörgen Sigvardsson4-Jul-06 14:53 
QuestionAdding scripting language to existing application ? Pin
Maximilien4-Jul-06 7:57
Maximilien4-Jul-06 7:57 
AnswerRe: Adding scripting language to existing application ? Pin
Chris Losinger4-Jul-06 8:09
professionalChris Losinger4-Jul-06 8:09 
Questionnamespace across multiple include files in library Pin
Joel Becker4-Jul-06 7:53
Joel Becker4-Jul-06 7:53 
AnswerRe: namespace across multiple include files in library Pin
Jun Du4-Jul-06 9:44
Jun Du4-Jul-06 9:44 
GeneralRe: namespace across multiple include files in library Pin
Joel Becker4-Jul-06 10:16
Joel Becker4-Jul-06 10:16 
GeneralRe: namespace across multiple include files in library Pin
Jun Du4-Jul-06 14:48
Jun Du4-Jul-06 14:48 
AnswerRe: namespace across multiple include files in library Pin
Joel Becker4-Jul-06 10:13
Joel Becker4-Jul-06 10:13 
Question.hh extension header files [modified] Pin
Jay034-Jul-06 7:09
Jay034-Jul-06 7:09 
AnswerRe: .hh extension header files Pin
toxcct4-Jul-06 7:31
toxcct4-Jul-06 7:31 
GeneralRe: .hh extension header files [modified] Pin
Jay034-Jul-06 7:42
Jay034-Jul-06 7:42 
GeneralRe: .hh extension header files Pin
toxcct4-Jul-06 7:43
toxcct4-Jul-06 7:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.