Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...

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

Is c++ a software?

238


What are friend functions in C++?

246


What is a singleton c++?

188


What is the use of setfill in c++?

210


Differentiate between a template class and class template in c++?

237






Is java made in c++?

204


Why do we use string in c++?

205


What is anonymous object in c++?

187


Which should be more useful: the protected and public virtuals?

236


What is c++ hiding?

199


What are the sizes and ranges of the basic c++ data types?

207


Using a smart pointer can we iterate through a container?

209


Can you pass a vector to a function?

192


What is a unnitialised pointer?

205


Define copy constructor.

230