C ++ operator overloading, assignment, deep copying and adding

I am currently studying operator overloading by re-reading some of my old university textbooks, and I think I misunderstand something, so I hope it will be some nice, lightweight reputation for some. responders If this is a duplicate, please point me in the right direction.

I created a simple counter class on which (at this stage) there is one member, val (int).

I initialized three of these varOne counters for varThree and want the third counter to be the sum of the first two (for example, varThree.val is set to 5 in the code below)

counter::counter(int initialVal)
{
    val = initialVal;
    //pVal = new int;
    //*pVal = 10; // an arbitrary number for now
}

int main (int argc, char const* argv[])
{
    counter varOne(3), varTwo(2), varThree;
    varThree = varOne + varTwo;

    return 0;
}

I overloaded the + operator like this:

counter operator+(counter& lhs, counter& rhs)
{
    counter temp(lhs.val + rhs.val);
    return temp;
}

I made this function not a member and friend of the counter class so that it can access private values.

pVal ( int). , varThree = varOne, , varOne , varThree.pVal .

operator= .

int counter::getN()
{
    return *newVal;
}

counter& counter::operator=(counter &rhs)
{
    if (this == &rhs) return *this;
    val = rhs.val;
    delete pVal;
    pVal = new int;
    *pVal = rhs.getN();
    return *this;
}

, - varThree = varOne, , varThree = varOne + varTwo :

counter.cpp: In function β€˜int main(int, const char**)’:
counter.cpp:96: error: no match for β€˜operator=’ in β€˜varThree = operator+(counter&, counter&)(((counter&)(& varTwo)))’
counter.cpp:55: note: candidates are: counter& counter::operator=(counter&)
make: *** [counter] Error 1

, counter::operator= operator+ operator= , , operator+, , , , , - .

+3
3

. :

counter& counter::operator=( const counter &rhs )

+(). , (-) . , , :

varOne + varTwo

. , , , op, , const.

, ( ).

+11

PImpl . , counter (int), operator =

counter& counter::operator=(const counter& rhs) {
  counter temp(rhs.getN());
  std::swap(&pVal,rhs.pVal);
  return *this;
}

, .

+1

( ), , ++ rvalues ​​ lvalues.

, , , : (, ), lvalue ( , ).

lvalue, rvalue - rvalues ​​ , "" , , . .

?

, ++ 98/03 (, , ), :

1) lvalue ( ) 2) rvalue ( )

:

// Consider the function foo below - it returns an int - 
// whenever this function is called, the compiler has
// to behave as if a temporary int object with the value 5 is returned.
// The use of 'foo()' is an expression that is an rvalue - try typing &foo() -
// [Note: if foo was declared as int& foo(), the story gets complicated, so
//   i'll leave that for another post if someone asks]

int foo() { return 5; }

void bind_r(int& r) { return; }
void bind_cr(const int& cr) { return; }

int main()
{
   int i = 10;  // ok
   int& ri = i; // ok binding lvalue to non-const reference, see rule #1
   int& ri2 = foo(); // Not ok, binding a temporary (rvalue) to a non-const reference 
        // The temporary int is created & destroyed by compiler here

   const int& cri = foo(); // ok - see rule #2, temporary int is NOT destroyed here

  //Similarly
   bind_r(i); // ok - rule #1
   bind_r(foo()); // NOT ok - rule #2
   bind_cr(foo()); // ok - rule #2


  // Since the rules above keep you out of trouble, but do not exhaust all possibilities
  // know that the following is well-formed too:
  const int& cri2 = i;
  bind_cr(i);
  bind_cr(cri);
  bind_cr(cri2); 

}

rvalue , ( ) ( ) - .

, , const, , .

p.s. (, , , ), , :)

p.s. , ++ 0x - rvalue ( const), rvalues ​​ ( ) - ++ - ;)

+1

Source: https://habr.com/ru/post/1709944/


All Articles