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 understand by zombie objects in c++?

620


Which is best c++ or java?

614


Assume studentNames and studentIDs are two parallel arrays of size N that hold student data. Write a pseudocode algorithm that sorts studentIDs array in ascending ID number order such that the two arrays remain parallel.

1721


What are guid? Why does com need guids?

570


What is the basic of c++?

606






Explain the use of virtual destructor?

626


Explain selection sorting?

639


What is a string example?

551


What is class invariant in c++?

748


What is the return value of the insertion operator?

606


Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the maximum number of concurrent threads that the InnoDB plug-in can create

2074


What is tellg () in c++?

727


What are inline functions? What is the syntax for defining an inline function?

583


What is a virtual destructor? Explain the use of it?

553


What is the difference between global int and static int declaration?

416