Hey, i was attempting an exercise on rvalue return functions(i think thats what you call them?)
I was given this class header file and required to implement the three functions:
1 2 3 4 5 6 7 8 9 10 11
Class NumberSafe{
int* _numberArr;
int _length;
public:
NumberSafe& operator=(const NumberSafe& pSafe);
NumberSafe& operator= (NumberSafe&& pSafe); //for this line shouldnt it be NumberSafe&& operator= ?
~NumberSafe( );
}
Please implement the 3 Functions so i could walk through it and understand it.
It depends on whether you want to do a shallow comparison of the classes or a deep comparison. A shallow comparison would compare the value at the address in memory pointed to by _numberArr. A deep copy would check if the same memory address is being pointed to by both pointers. I implemented the deep copy way.
On line 3 you're deleting the memory used by an array. In the original class implementation you only have a pointer to an int. How should I have known to delete an array?
And you implemented the two functions wrong--they have a design flaw--fir example, when you assign a class with the operator=, you don't want to get rid of the class being assigned.
I am only slightly familiar with rvalue references--only enough that I know what they look like and I have a vague idea about how they work. To my (inexperienced) eyes, your code looks about right and I also think the return value for Operator=(NumberSafe&&) should be NumberSafe&& based on the way you wrote it.
I'm not sure if a NumberSafe&& return value makes sense. I hope someone more familiar with rvalue references and move semantics chimes in with better help.