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 flag in computer?

593


Will this c++ program execute or not?

595


Is c or c++ more useful?

566


What is encapsulation in c++ with example?

576


What is ctime c++?

566






How would you find out if a linked-list is a cycle or not?

538


Is c++ used anymore?

576


How compile and run c++ program in turbo c++?

618


Which operations are permitted on pointers?

560


Explain the term memory alignment?

668


Which software is best for programming?

652


Can comments be nested?

619


Why c++ is the best language?

586


What is microsoft c++ redistributable?

604


What are the rules for naming an identifier?

549