Can destructor be private?

Showing Answers 1 - 27 of 27 Answers

Vijay Roy

  • Jan 25th, 2006
 

No, Destructor should be public If you want to instansitate the object.

  Was this answer useful?  Yes

Swapnil

  • Jan 30th, 2006
 

Yes destructors can be private. But according to Standard Programming practise it is not advisable to have destructors to be private.

  Was this answer useful?  Yes

abcelango

  • Feb 13th, 2006
 

Yes, but if the destructors are private, the class cannot be instantiated.

  Was this answer useful?  Yes

anon

  • 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.

malathi

  • 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.

bappa

  • 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.

  Was this answer useful?  Yes

@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;

}

  Was this answer useful?  Yes

sudheer

  • 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

  Was this answer useful?  Yes

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 <iostream>

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;
}

  Was this answer useful?  Yes

lukah

  • 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

  Was this answer useful?  Yes

Aniket

  • 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..

  :-)

  Was this answer useful?  Yes

Phani Dasam

  • Mar 29th, 2007
 

If the class is a singleton class, then we can make the constructor and destructor as private.

  Was this answer useful?  Yes

yzesong

  • Aug 1st, 2009
 

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. 

  Was this answer useful?  Yes

Gaurav

  • Jul 15th, 2011
 

destructor has nothing to do with class instantiation....

  Was this answer useful?  Yes

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
  1. #include <iostream>

  2. #include <stdlib.h>

  3. #include <tchar.h>

  4.  

  5. using namespace std;

  6.  

  7. class A

  8. {

  9. public:

  10.         static A * abc;

  11.  

  12.         A()

  13.         {

  14.                 abc = this;

  15.                 atexit(Clear);

  16.         }

  17. private:

  18.         ~A()

  19.         {

  20.                 cout << "Deleting A" << endl;

  21.         }

  22.        

  23.         static void Clear()

  24.         {

  25.                 delete(abc);

  26.         }

  27. };

  28.  

  29. A* A::abc = NULL;

  30.  

  31.  

  32. int _tmain(int argc, _TCHAR* argv[])

  33. {

  34.         A *a = new A();

  35.  

  36.         return 0;

  37. }


  Was this answer useful?  Yes

NikunjSingh

  • Feb 22nd, 2012
 

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
  1. #include <iostream>

  2.  

  3. using namespace std;

  4.  

  5. class Shape{

  6.         ~Shape(){}

  7. public:

  8.         Shape(){cout<<"Shape..."<<endl;}

  9.         Shape(const Shape& obj)

  10.         {

  11.                 cout<<"Copy called"<<endl;

  12.         }

  13.         void* operator new(size_t size)

  14.         {

  15.                 cout<<"new...."<<endl;

  16.                 void* storage=malloc(size);

  17.                 if(NULL == storage) {

  18.                         throw "allocation fail : no free memory";

  19.                 }

  20.                 return storage;

  21.         }

  22.         static void create(){

  23.                 Shape obj;

  24.         }

  25.         void operator delete (void*){

  26.  

  27.         }

  28. };

  29.  

  30. int main()

  31. {

  32. //      Shape obj; //error

  33. //      Shape obj2=obj;//error

  34. //      Shape obj3(obj);//error

  35.         Shape* ptrShape=new Shape();

  36.         Shape* ptrShapeOver= new Shape();

  37.         Shape::create();

  38.         return 0;

  39. }

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions