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

Do you know what are pure virtual functions?

642


How many keywords are used in c++?

560


Can we use this pointer inside static member function?

630


What are keywords in c++?

602


What is namespace std; and what is consists of?

668






What is near, far and huge pointers? How many bytes are occupied by them?

663


What is a try block?

642


Can a program run without main?

633


Distinguish between new and malloc and delete and free().

575


Does a derived class inherit or doesn't inherit?

620


Why are pointers not used in c++?

628


What is the importance of mutable keyword?

585


What is a static element?

577


Is multimap sorted c++?

559


What is a node class in c++?

648