Vijay Roy
Answered On : Jan 25th, 2006
No, Destructor should be public If you want to instansitate the object.
Login to rate this answer.
Swapnil
Answered On : Jan 30th, 2006
Yes destructors can be private. But according to Standard Programming practise it is not advisable to have destructors to be private.
Login to rate this answer.
abcelango
Answered On : Feb 13th, 2006
Yes, but if the destructors are private, the class cannot be instantiated.
Login to rate this answer.
Yes, destructor can be private and it is not true that class cannot be instantiated. Use friend functions.

1 User has rated as useful.
Login to rate this answer.
anon
Answered On : Feb 27th, 2006
Dudes. Don't be silly.
Private destructors are used to prevent contruction in automatic or static context. This is helpful if your destructor calls delete.
Login to rate this answer.
malathi
Answered On : Apr 30th, 2006
hi
The simple answer to why the destructor should be made private is that theprogrammer doesn't want anyone to be able to destroy the object.
And the private destructor behavior depends upon the compiler in use. The GNU compiler collection does not allow inheritance from a class, whose destructor is private.
If inheritence is required then the destructor should be made protected.

2 Users have rated as useful.
Login to rate this answer.
bappa
Answered On : Jun 6th, 2006
costructor , destructor can be private......that make the class final class i.e......this class can't be inherited(because canot be instenteniet from out side of the class). Only one static method in that class can create obj. and return for use.
Login to rate this answer.
@BAPPA :- TRY THIS
#include
<
iostream>
class
Der1;
class
ABC{
public
:
virtual void showABC(void)
{
std::cout<<std::endl<<"showABC inside the ABC base"<<std::endl;
}
virtual void showCurrentClass(void) =0;
friend class Der1;
private
:
virtual ~ABC() = 0{}
};
class
Der1:
public ABC{
public
:
virtual void showCurrentClass(void)
{
std::cout<<std::endl<<"Der1 inside the Der1"<<std::endl;
}
virtual ~Der1()
{
}
};
int
main(
int argc,
char*
argv[])
{
ABC * d1 = new Der1;
d1->showCurrentClass();
d1->showABC();
return 0;
}
Login to rate this answer.
sudheer
Answered On : Aug 27th, 2006
Hi @mohit.. In the above example which you have given is absolutely fine. But you have missed this statement : delete d1; When you say this it tries to access the private member of base destructor which leads to compilation error Regards, Sudheer
Login to rate this answer.
Hi @sudheer ...
Thanks for ur valuable reply but the answer I had posted is not for whether class which having the private destructor is workable or not. But I had posted answer for the class having private destructor can be inherited or not.If u r more conscious abt memory cleaning then direct call to delete operator will give compile time error but u can make by another way...Even though we know we r discussing on nonsense query coz no body really wanna to keep their class's destructor as private but I am giving u code pls go through it.
this code I have added constructor of class ABC and Der1 , member variable int m_iVal; in base and virtual void clean() function in both classes.
#include
class Der1;
class ABC
{
public:
int m_iVal; /*ADDED*/
ABC(int i):m_iVal(i){} /*ADDED*/
virtual void showABC(void){
std::cout<<std::endl<<"showABC inside the ABC base"<<std::endl;
}
virtual void showCurrentClass(void) =0;
friend class Der1;
public:
virtual ~ABC() = 0{}
virtual void clean() = 0;/*ADDED*/
};
class Der1:public ABC
{
public:
Der1(int i = 100):ABC(i){}/*ADDED*/
virtual void clean(){/*ADDED*/
Der1 * d = this;
delete d;d = NULL;
}
virtual void showCurrentClass(void){
std::cout<<std::endl<<"Der1 inside the Der1"<<std::endl;
}
virtual ~Der1(){}
};
int main(int argc, char* argv[])
{
ABC * d1 = new Der1(200);
d1->showCurrentClass();
d1->showABC();
printf("nValue of I is i = %d n",d1->m_iVal);/*ADDED*/
d1->clean();/*ADDED*/
printf("n value of I is garbage i = %d n",d1->m_iVal);/*ADDED*/
return 0;
}
Login to rate this answer.
lukah
Answered On : Sep 20th, 2006
Yes it can be. Look at this section of code:class A {public: A() {};virtual ~A() {};};class B:publicA {~B() {};public:B() {}};main(){A *a=new B;}You will see it works and it calls constructors and destructors like this : A, B, ~B, ~A
Login to rate this answer.
Aniket
Answered On : Oct 8th, 2006
hi
yes Destructor can b private,
but it is not actually the right way ,so it is bettr to keep it public..
:-)
Login to rate this answer.
Phani Dasam
Answered On : Mar 29th, 2007
If the class is a singleton class, then we can make the constructor and destructor as private.
Login to rate this answer.
My answer is, since constructor can be private, destructor can be private too. It is called implicitly, so it won't matter if it is public or private.
Login to rate this answer.
Gaurav
Answered On : Jul 15th, 2011
destructor has nothing to do with class instantiation....
Login to rate this answer.
YESSSS destructors can be private.
Infact this concept is used in Singleton design pattern.
So, outsiders cannot delete object of that class. It has to b deleted from the class itself.
I know...It sounds strange... How can one destroy itself.. But it is possible...
Try this code..
Code
#include <iostream>
#include <stdlib.h>
#include <tchar.h>
using namespace std;
class A
{
public:
static A * abc;
A()
{
abc = this;
atexit(Clear);
}
private:
~A()
{
cout << "Deleting A" << endl
;
}
static void Clear()
{
delete(abc);
}
};
A* A::abc = NULL;
int _tmain(int argc, _TCHAR* argv[])
{
A *a = new A();
return 0;
}
Login to rate this answer.
Destrcutors can be made private to stop user from creating objects on stack.
One can still create objects on stack using a static function as member function. But if intention is not to let user create objects on stack, you should not create such a func.
Code
#include <iostream>
using namespace std;
class Shape{
~Shape(){}
public:
Shape(){cout<<"Shape..."<<endl;}
Shape(const Shape& obj)
{
cout<<"Copy called"<<endl;
}
void* operator new(size_t size)
{
cout<<"new...."<<endl;
void* storage=malloc(size);
if(NULL == storage) {
throw "allocation fail : no free memory";
}
return storage;
}
static void create(){
Shape obj;
}
void operator delete (void*){
}
};
int main()
{
// Shape obj; //error
// Shape obj2=obj;//error
// Shape obj3(obj);//error
Shape* ptrShape=new Shape();
Shape* ptrShapeOver= new Shape();
Shape::create();
return 0;
}
Login to rate this answer.