GeekInterview.com
Series: Subject: Topic:
Question: 203 of 267

Can destructor be private?

Asked by: Interview Candidate | Asked on: Jan 21st, 2006
Showing Answers 1 - 17 of 17 Answers
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.
wlochaty

Answered On : Feb 14th, 2006

View all answers by wlochaty

Yes, destructor can be private and it is not true that class cannot be instantiated. Use friend functions.

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

Yes  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.
yzesong

Answered On : Aug 1st, 2009

View all answers by yzesong

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

  
Login to rate this answer.
NikunjSingh

Answered On : Feb 22nd, 2012

View all answers by NikunjSingh

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

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.