Implemention of rvalue return function

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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Numbersafe::NumberSafe& operator=(const NumberSafe& pSafe)
{
_length = pSafe._length;
_numberArr = &pSafe._numberArr;
}

Numbersafe::NumberSafe& operator==(const NumberSafe& pSafe)
{
return pSafe._length == _length && _numberArr == pSafe._numberArr;
}

Numbersafe::NumberSafe& operator=(const NumberSafe& pSafe)
{
delete _numberArr;
}


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.
I assumed it would look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
NumberSafe::NumberSafe& Operator=(const NumberSafe& pSafe){
If (this != pSafe){
     delete [] _numberArr;
     _numberArr = nullptr;
     _length = 0;
  
   if (pSafe._numberArr != nullptr && pSafe._length > 0){
       _length = pSafe._length;
       _numberArr = new int [pSafe._numberArr];

       for ( int i = 0; i < pSafe._length; i++){
            _numberArr[i] = pSafe._numberArr[i]
     }
  }

}

NumberSafe::NumberSafe& Operator=(NumberSafe&& pSafe){

    if (&pSafe != this){
        _numberArr = pSafe._numberArr;
        _length = pSafe._length;
        pSafe._numberArr = nullptr;
        pSafe._length = 0;
}
return std::move(*this);
}
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.

BTW, where did you get this code from?
I got this constructed this code from my Course textbook.
I would look up some material on operator overloading--http://en.cppreference.com/w/cpp/language/operators
@theturk1234,

You need to look up some material on rvalue references and move semantics. This is a pretty good article.

http://thbecker.net/articles/rvalue_references/section_01.html
@andirew,

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.
Topic archived. No new replies allowed.