Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
4.60/5 (5 votes)
See more:
Hi

I was just doing simple things in destructor but i found something which is not clear to me.

here the code below
C++
#include "stdafx.h"
#include "conio.h"
class cFather
{
	public:
	virtual ~cFather()
	{
		printf("\n~cFather()");	//dont ask me why i used printf instead cout	
	}
};

class cSon : public cFather
{
	~cSon()
	{
		printf("\n ~cSon()");		
	}
};


int _tmain(int argc, _TCHAR* argv[])
{
	class cFather * pObjFthr = new cSon();
	delete pObjFthr;
	getch();
	return 0;
}



output
~cSon()
~cFather();

Question:
As you can see the destructor in cSonClass is private but being called.
I just wnt to know the mechanishm what is running under the hood.

Thank in advance.
Posted
Updated 20-Aug-12 21:20pm
v2
Comments
stib_markc 21-Aug-12 3:22am    
If you understand inheritance and control flow during object creation and object destruction then first solution(by SA) will answer your question.
kaushik4study 21-Aug-12 3:32am    
@stib_markc i am bit confused in the solution given by -SA. I understand when we use delete on heap the appropriate destructor will be called but i guess(i know i am wrong but dont know where) the destructor should be public. same as if you remove the virtual keyword from cFather clas the it will throw an error which should be the same case for cSon class. please tell me i am really missing something.
Thanks
stib_markc 21-Aug-12 4:51am    
First you have to understand what does the word 'virtual' means. And irrespective of the access specifier(public or private)/modifier(virtual or not), a destructor will be called when you call "delete"(for objects created on heap) or when the stack variables go out of scope. Please read polymorphism and inheritance and how they are used on constructor and destructor.

Actually destructor has no special role in that, try, for instance
C++
#include <iostream>

class A
{
public:
  virtual void show()=0;
};

class B: public A
{
private:
  virtual void show(){ std::cout << "hi" << std::endl;}
};

int main()
{
  A *p = new B();
  p->show();
}


The point you spotted comes from polymorphism (or, better, as SAK noted, from late binding) and as strange as it looks, it is the correct behaviour, see , for instance Public virtual function derived private in C++[^] at Stack Overflow.
 
Share this answer
 
Comments
Wendelius 21-Aug-12 17:56pm    
Good answer!
kaushik4study 22-Aug-12 1:48am    
Wow never thought in this way. thanx man :)
Ashish Tyagi 40 22-Aug-12 9:24am    
Good one my 5.
Destructors are called automatically when an instance is deleted (in case when heap is used) or goes out of some block (in case of a local variable). They don't have to be accessible from outside of the class/structure.

[EDIT #1]

On OP's request, I'm adding some links:
http://www.learncpp.com/cpp-tutorial/86-destructors/[^],
http://www.learncpp.com/cpp-tutorial/69-dynamic-memory-allocation-with-new-and-delete/[^].

I would advise you to read through all of the manual before you process — it can be very useful. I could be some other manual or a book though. This one is especially good to have:
http://www.amazon.com/The-Programming-Language-Special-Edition/dp/0201700735/ref=sr_1_1?ie=UTF8&qid=1345580932&sr=8-1&keywords=Stroustrup+C%2B%2B[^],
http://www.amazon.com/C-Programming-Language-4th-Edition/dp/0321563840/ref=sr_1_3?ie=UTF8&qid=1345580932&sr=8-3&keywords=Stroustrup+C%2B%2B[^],
http://www.amazon.com/Primer-Plus-Edition-Developers-Library/dp/0321776402/ref=sr_1_8?ie=UTF8&qid=1345580932&sr=8-8&keywords=Stroustrup+C%2B%2B[^].

[EDIT #2]

Another useful manual:
http://www.cplusplus.com[^].

And links on the topics being discussed:
http://www.cplusplus.com/doc/tutorial/classes/[^],
http://www.cplusplus.com/doc/tutorial/dynamic/[^].

—SA
 
Share this answer
 
v3
Comments
nv3 21-Aug-12 3:23am    
5 ed. What probably surprised kaushik4study is the failing symmetry. To instantiate an object one must well have access to the constructor. Well observed, kaushik4study!
Sergey Alexandrovich Kryukov 21-Aug-12 3:26am    
As there is a "fool's infinity" know from philosophy, there could be some "fool's symmetry". There is no such thing in the OOP; that said, it's just because it's not totally foolish... :-)
Thank you very much,
--SA
CPallini 21-Aug-12 3:34am    
That's not the whole picture. There is polymorphism here too. The compiler (at least GCC) does not allow the direct contruction of cSon, that is
cSon * ps = new cSon();
Sergey Alexandrovich Kryukov 21-Aug-12 13:20pm    
You should have said "late binding". The term "polymorphism" is only used in consideration of a set of object in case of more than one objects with possibility of having more than one run-time type. Besides, cSon is directly constructed because it's the case when the default constructor is provided, right?
--SA
CPallini 21-Aug-12 15:39pm    
'Late binding' is the technique that allows polymorphism (at least ) in C++.
With 'direct construction' I meant: "without cFather class you couldn't have an instance of cSon".
// I just wnt to know the mechanishm what is running under the hood.

All derived destructors will be virtual (automatically)
since it is so at the base,

in this case - any protection has no role :)
 
Share this answer
 
Comments
kaushik4study 21-Aug-12 8:14am    
are you sure because inheritance with virtual function do not follow this concept. they do care what is the scope of the fucntion.
I am not arguing but could not get the right answer.
Eugen Podsypalnikov 21-Aug-12 9:00am    
Sure :) , that is why you can see the "~cSon()" output. Try it without the keyword virtual in the base, and you will win a memory leak :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900