Do we have private destructors?

Answer Posted / binoy mathew

#include <iostream>
#include <stdlib.h>

class t
{
public:
t()
{
printf("in constr");
}

private: // note that constructor is private here
~t()
{
printf("in destr");
}
};


int main()
{
t *t1 = new t; // create a new obj
delete t1; // delete the obj, which calls destructor
return 0;
}

Try to compile the above code.
following error results...

[root@localhost Desktop]# g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:13: error: ‘t::~t()’ is private
tt.cpp:: error: within this context
[root@localhost Desktop]#

....implies, we can't have destructor private.

Is This Answer Correct ?    1 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How do I run a program in notepad ++?

612


In inline " expression passed as argument are evalauated once " while in macro "in some cases expression passed as argument are evaluated more than once " --> i am not getting it plz help to make me understand....

1959


What language is a dll written in?

554


What is DlgProc?

608


What is lambda in c++?

589






Explain the concept of copy constructor?

631


What is polymorphism in c++? Explain with an example?

619


Explain the static member function.

723


What does flush do?

568


Explain how we implement exception handling in c++?

583


What is ofstream c++?

595


What is std :: flush?

590


What is the best c++ book?

720


Can circle be called an ellipse?

648


What is the use of endl in c++?

605