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

What do you know about near, far and huge pointer?

582


what does the following statement mean? int (*a)[4]

604


List the advantages of inheritance.

628


What are the types of container classes?

604


What is the meaning of c++?

530






What is function overloading c++?

560


How do you flush a buffer in c++?

587


Which format specifier is used for printing a pointer value?

561


Can a program run without main in c++?

571


How can you quickly find the number of elements stored in a dynamic array?

559


What is the use of ::(scope resolution operator)?

640


Is java the same as c++?

535


Explain how overloading takes place in c++?

560


What is const pointer and const reference?

579


What is protected inheritance?

585