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
What is the difference between public, private, and protected access?
Why do we need templates?
what does the following statement mean? int (*a)[4]
If there are two catch statements, one for base and one for derived, which should come first?
Tell me an example where stacks are useful?
Why do we use the using declaration?
What is the keyword auto for?
Is java easier than c++?
Which of the following is evaluated first: a) && b) || c) !
Should I learn c or c++ first?
What is the history of c++?
Which compiler does turbo c++ use?
What is a flag in c++?
When to use “const” reference arguments in a function?
What is constant in c++ with example?