A
Aneel
What does the calling of destructor at the end of assignment operator
account for? Please see the code below:
#include <iostream>
using namespace std;
class check {
private:
int x;
public:
check(int a=0):x(a){cout<<"\nconstructor\n";}
~check(){cout<<"\ndestructor\n";}
void set_x(int a){x=a;}
int get_x(){return x;}
check operator = (const check &rhs) {
cout<<"\nassignment operator\n";
x = rhs.x;
}
};
int main() {
check ob1, ob2(3);
cout<<"\n\n----------------------\n";
ob1 = ob2;
cout<<"\n------------------------\n\n"; // Destructor is called
before reaching this line, WHY?
system("pause");
return 0;
}
account for? Please see the code below:
#include <iostream>
using namespace std;
class check {
private:
int x;
public:
check(int a=0):x(a){cout<<"\nconstructor\n";}
~check(){cout<<"\ndestructor\n";}
void set_x(int a){x=a;}
int get_x(){return x;}
check operator = (const check &rhs) {
cout<<"\nassignment operator\n";
x = rhs.x;
}
};
int main() {
check ob1, ob2(3);
cout<<"\n\n----------------------\n";
ob1 = ob2;
cout<<"\n------------------------\n\n"; // Destructor is called
before reaching this line, WHY?
system("pause");
return 0;
}