Do we have private destructors?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is binary search in c++?

769


What is the use of this pointer in c++?

755


What is a stack c++?

743


Is java a c++?

755


What are the two types of polymorphism?

764






Why is c++ considered difficult?

810


. If employee B is the boss of A and C is the boss of B and D is the boss of C and E is the boss of D. Then write a program using the Database such that if an employee name is Asked to Display it also display his bosses with his name. For eg. If C is displayed it should also display D and E with C?

2994


What do you mean by storage classes?

1101


What is searching? Explain linear and binary search.

761


How to get the current position of the file pointer?

758


Which c++ operator cannot overload?

728


Can we use this pointer inside static member function?

804


Why isn't sizeof for a struct equal to the sum of sizeof of each member?

719


What is a vector c++?

806


How is data hiding achieved in c++?

755