Hi need to implement assignment operator for class TFoo below such that the code in main will operate as indicated.
I have these assumptions:
a) the constructor/destructor code for Foo has been omitted; I am to assume that it is properly implemented
b) the 'i_myData' member is correctly assigned and destroyed in the omitted code
I have these restrictions:
I may add methods to the TFoo class, but I should not modify existing methods
I have to ensure that memory/resources are cleaned-up
I'd appreciate some help,
Thanks.
Code:
class Data {
// implemented elsewhere
};
class TFoo {
int& i_Value;
Data* i_myData; // initialized in constructor
public:
TFoo(int& v) : i_Value(v) {
// implemented elsewhere
}
~TFoo() {
// implemented elsewhere
}
void Increment() { ++i_Value; }
//tried this but not correct!
TFoo & operator= (const TFoo & other) {
if (this != &other)
{
i_Value = other.i_Value;
}
return *this;
}
};
int main()
{
int i = 0;
int j = 1;
int k = 3;
TFoo foo1(i);
TFoo foo2(j);
foo1.Increment();
foo2.Increment();
printf("%d,%d,%d\n", i, j, k); // displays "1,2,3"
foo1 = foo2;
foo2 = k;
foo1.Increment();
foo2.Increment();
printf("%d,%d,%d\n", i, j, k); // displays "1,3,4"
return 0;
}