1

This function is part of a larger project for my c++ class. When I try to run it on visual c++ 2017 I get the following warning:

warning C4172: returning address of local variable or temporary: temp

Item& Item::operator++(int n) {
    //Increments the code to be printed on the next insertion and returns the 
    //value before incrementation.

    Item temp = *this;
    code++;
    return temp;
}

Is there a way to remove this warning and still return the value before the incrementation? Thanks.

tadman
  • 208,517
  • 23
  • 234
  • 262
elvisi27
  • 29
  • 1
  • 9
  • 3
    Just do `code++;return *this;` – Jake Freeman Dec 14 '17 at 03:35
  • 1
    Can you explain your requirement to not change the header? The proper way to overload post-increment is to return a copy of the object made before the increment by value. https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading – Retired Ninja Dec 14 '17 at 03:48
  • 1
    If you really want to just get rid of the warning without changing the header, here is a terrifying and broken way to do so. https://ideone.com/fksOVY – Retired Ninja Dec 14 '17 at 04:01
  • @RetiredNinja Thanks man. I know it looks ugly and its not an efficient way of doing it but my assignment specifications does not allow me to edit function headers , I just have to implement them. – elvisi27 Dec 14 '17 at 04:13
  • You realize it's completely unsafe right? Whoever gave you the assignment declared the function incorrectly. I'd tell them. – Retired Ninja Dec 14 '17 at 04:57
  • *I just have to implement them* -- I think a lot of the C++ teaching community is purposefully sabotaging any hope of C++ being a language persons want to use. – PaulMcKenzie Dec 14 '17 at 05:28

1 Answers1

2

The warning is correct. You're trying to return a reference to local object, which will be destroyed when get out of the function, with the dangled reference left.

return the value before the incrementation?

I suppose you should change it to return-by-value; the idea of post-increment operator is to return a copy before performing increment. i.e.

Item Item::operator++(int n) {
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • @songyuanyao Thanks for the answer, but I am not allowed to change the function header, is there any other workaround? – elvisi27 Dec 14 '17 at 03:39
  • @JakeFreeman For [post-increment operator](http://en.cppreference.com/w/cpp/language/operator_incdec), you should. – songyuanyao Dec 14 '17 at 03:41
  • @elvisi27 Returning reference for [post-increment operator](http://en.cppreference.com/w/cpp/language/operator_incdec) doesn't make sense to me. Anyway, if you have to do, you could use `new` to prevent the object to be destroyed.. but it's really a bad idea. – songyuanyao Dec 14 '17 at 03:50
  • There was an answer earlier: Item * temp = this; code++; return *temp; Is that supposed to work ? – elvisi27 Dec 14 '17 at 03:52
  • 1
    @elvisi27 No, `temp` and `this` points to the same object, so it's same as `return *this;`, i.e. return the object after incrementation. – songyuanyao Dec 14 '17 at 03:54