K
Kumar Anurag
Implement a class which can only have two objects e.g. obj1,obj2.Now
when obj3 is created ,then obj1 should get destroyed.. This process
will be repeated . It means at any instant we will have only two
objects.
I did is using copy constructor is it right??
#include<iostream>
using namespace std;
class A{
public:
A(A& Old){
Old.~A();//here we destroyed the old object2("b")
}
};
int main()
{
A a; //Object1
A b; //Object2
A c(b);//Suppose i want to create object3 "c"
//Here we pass that object in parameter which we want to get
//Destroyed (here i want Object2("b") to get destroyed;
return 0;
}
/*whenever i need to create new object i will pass as
Format:-> A newObject(oldObject);
*/
is it correct??/
when obj3 is created ,then obj1 should get destroyed.. This process
will be repeated . It means at any instant we will have only two
objects.
I did is using copy constructor is it right??
#include<iostream>
using namespace std;
class A{
public:
A(A& Old){
Old.~A();//here we destroyed the old object2("b")
}
};
int main()
{
A a; //Object1
A b; //Object2
A c(b);//Suppose i want to create object3 "c"
//Here we pass that object in parameter which we want to get
//Destroyed (here i want Object2("b") to get destroyed;
return 0;
}
/*whenever i need to create new object i will pass as
Format:-> A newObject(oldObject);
*/
is it correct??/