Do we have private destructors?
Answers were Sorted based on User's Feedback
Answer / sampurna pandey
yes, we can have both private constructor and destructor
and can use them with help of static function for example
class Base
{
static int i;
Base(){}
~Base(){}
public:
static Base *Creat_Object();
static void Destroy_Object(Base *p);
void Add()
{
cout<<i<<" Add function"<<endl;
}
};
int Base::i=0;
Base* Base::Creat_Object()
{
Base * ptr = new Base();
i=5;
return ptr;
}
void Base::Destroy_Object(Base *p)
{
delete p;
}
void main()
{
Base *temp = Base::Creat_Object();
temp->Add();
Base::Destroy_Object(temp);
}
correct me if i am wrong.
Is This Answer Correct ? | 21 Yes | 3 No |
Answer / guest
RE: #1
You can have private constructors. It's pretty common
practice when you create a class which you want to never be
constructed with "new" from outside the class (for example,
if you have a static factory member of the class).
Is This Answer Correct ? | 10 Yes | 1 No |
Answer / vaibhav meena
We can have Private Destructor and by using Friend Function
we can also instantiate it. :)
Is This Answer Correct ? | 6 Yes | 0 No |
Answer / subrat
Hi all,
Please visit this url for more clarity on "Private
destructors"
http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c414
3/
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / 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 |
Answer / shakti singh khinchi
U can't instantiate instance of that class.
Is This Answer Correct ? | 0 Yes | 3 No |
Answer / vishwa
we cannot say it destructors are not private. because for
empty class if we use delete operator then it is going to
delete the destructor which is private.
Is This Answer Correct ? | 0 Yes | 8 No |
Answer / thuang
In replay to Vishwa,
for empty class, compiler will supply a default constructor
and a destructor for us, their access modifiers were public.
Is This Answer Correct ? | 0 Yes | 8 No |
Answer / pooja
no we do not have private destructors because the
destructors can be declared only in public access
specifier.
Is This Answer Correct ? | 1 Yes | 11 No |
What are the advantages of c++ over c?
Which is the best c++ compiler for beginners?
Evaluate !(1&&1||1&&0) a) Error b) False c) True
What is c++ course?
Describe private, protected and public?
If dog is a friend of boy, and terrier derives from dog, is terrier a friend of boy?
Consider the following code fragment: int main(void) { int m = 4; mystery ( m ); mystery ( m ); printf("%d", m); return 0; } What is the output on the monitor if mystery is defined as follows ? void mystery (int m) { m = m+3; }
When there is a global variable and local variable with the same name, how will you access the global variable?
Can we overload operator in c++?
Read the following program carefully and write the output of the program. Explain each line of code according to given numbering. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> 1……………… int main (void) { pid_t pid; 2………………………… pid = fork(); 3…………………………. if (pid > 0) { int i; 4………………………… for (i = 0; i < 5; i++) { 5………………… …………… printf(" I AM VU : %d\n", i); 6………………… …………… sleep(1); } exit(0); } 7………………… ……… else if (pid == 0) { int j; for (j = 0; j < 5; j++) { 8……………………………… printf(" I have no child: %d\n", j); sleep(1); } _exit(0); } else { 9………………………………fprintf(stderr, "can't fork, error %d\n", errno); 10……………… … ………… exit (EXIT_FAILURE); } }
Can you explicitly call a destructor on a local variable?
What is c++ and its features?