2

I want to overload the ++ operator but it isn't working. The example I found in the book I'm using used stack memory allocation and I tried to do it through heap memory allocation. It isn't crashing but it isn't incrementing either.

I tried returning the pointer, making a reference, all sorts of stuff that I don't quite understand yet and nothing really worked.

#include <iostream>

using namespace std;

class MyObject{
public:
  MyObject(int initVal = 0):value(initVal){cout << "Constructor called..." << endl;}
  ~MyObject(){cout << "Destructor called..." << endl;}
  const MyObject& operator++();
  int getValue(){return value;}
private:
  int value = 0;

};

int main(){
  int initVal = 0;
  char done = 'N';

  cout << "initVal?" << endl;
  cin >> initVal;
  MyObject *obj = new MyObject(initVal);
  while(done == 'N'){
    cout << "Current value of obj :" << obj->getValue() << ". Want to stop? (Y/N)" << endl;
    cin >> done;
    //cout << "value of done :" << done << endl;
    //cin.get();
    if(done != 'Y' || done != 'N'){
      continue;
    }
    *obj++;
  }
  cout << "";
  cin.get();
}

const MyObject& MyObject::operator++(){
  cout << "OVERLOADER CALLED val:" << value << endl;
  value++;
  return *this;
}

Actual:

initVal?
10
Constructor called...
Current value of obj :10. Want to stop? (Y/N)
N
Current value of obj :10. Want to stop? (Y/N)
N
Current value of obj :10. Want to stop? (Y/N)
N
Current value of obj :10. Want to stop? (Y/N)

Expected:initVal?
10
Constructor called...
Current value of obj :10. Want to stop? (Y/N)
N
Current value of obj :11. Want to stop? (Y/N)
N
Current value of obj :12. Want to stop? (Y/N)
N
Current value of obj :13. Want to stop? (Y/N)
Y

Also, my test to see if the response was not either Y or N stops the program when true instead of iterating at the beginning of the while loop. Help on that is also appreciated.

Versalys
  • 55
  • 8
  • 1
    You need `(*obj)++` but you shouldn't be using a pointer anyway. – David G May 27 '19 at 20:05
  • 1
    1) [Operator precedence](https://en.cppreference.com/w/cpp/language/operator_precedence) 2) "_It isn't crashing but it isn't incrementing either._" What said, that the code should crash, if it exhibits undefined behavior? It may crash, or it may seem to do exactly what you want, or it might crash after unspecified amount of time, in an unspecified place. – Algirdas Preidžius May 27 '19 at 20:08
  • You mentioned a book, if you want more of them then take a look at [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Quimby May 27 '19 at 20:27
  • This doesn’t address the question, but can you think of any values of `done` for which the value of the expression `done != ‘Y’ || done != ‘N’` is false? Also, a `continue` as the last statement in a loop is pointless — the loop will continue without it. – Pete Becker May 28 '19 at 01:04

1 Answers1

5

You've fallen victim to operator precedence. The expression *pointer++ dereferences the pointer, returns that reference and increments the pointer, not the value. It's equivalent to *(pointer++).

The solution is to add a pair of parentheses: (*pointer)++.

Don't use new, std::unique_ptr is the correct way to handle dynamic memory.

Also you overloaded the prefix operator, you probably wanted postfix. Both operators should look something like this:

MyObject MyObjects::operator++(int)//Post-fix accepts unused int argument
{
    MyObject copy{*this};
    ++*this; // Use prefix++ to avoid redundant code.
    return copy;
}

MyObject& MyObjects::operator++()
{
    //Put incrementing logic here
    ++this->value;
    return *this;
}
Quimby
  • 17,735
  • 4
  • 35
  • 55
  • _"The expression *pointer++ dereferences the pointer, returns that reference and increments the pointer, not the value"_ this is not entirely correct. See [this example](https://godbolt.org/z/p5hdx5). What happens is that the pointer is incremented before it is dereferenced. But because `pointer++` is a postfix increment, the old value of the pointer is returned which is then dereferenced. – Timo May 27 '19 at 20:47