I'm using codeblocks/mingw gcc as my compiler and for the following code I'm getting a warning.
overloadplusminus.cpp|23|warning: no return statement in function returning non-void|
How come only one of the overloaded operators for ++ tell me that there is no return statement. I know that one is postfix and the other is prefix but why is there that preference. I actually don't need to return anything. I'm trying to make an iterator with this.
EDIT::NM I just realized there is a type number in front of the declaration for the function thats supposed to return a type number and the other one is type void.
Heres a link to parashit [www.parashift.com]
operator++() is prefix and operator++(int) is postfix.
The ++ operation should return the object itself.
Your code works fine with no errors or warnings. I test it with both clang++ and g++.
number& number::operator++() { value++; }
This doesn't return anything.
postfix should return a copy of *this before it is incremented. Otherwise you just used prefix.
This looks like an issue of learning a C++ subtlety that's rarely used (operator overloading), while the basics of the language are still not clear.
i++ evaluates to "the value of i before incrementation"
++i evaluates to "the value of i after incrementation"
i=5 evaluates as five.
postfix should return a copy of *this before it is incremented. Otherwise you just used prefix.
Actually no try this one.
This looks like an issue of learning a C++ subtlety that's rarely used (operator overloading), while the basics of the language are still not clear.
Its meant to be used for iterating a list.
Its meant to be used for iterating a list.
Even if you only use it in a for(;;num++) loop, there's very little difficulty in letting it evaluate as the right value for a ++ operator.
ie. You should make this compile and run with the expected results:
number eight(8); height.print(); // expected: 8 (eight++).print(); // expected: 8 (++eight).print(); // expected: 10
Even if you only use it in a for(;;num++) loop, there's very little difficulty in letting it evaluate as the right value for a ++ operator.
ie. You should make this compile and run with the expected results:
I'm not sure what you're saying, is the code correct or do I need to fix something?
EDIT:: I tried to compile the code but it wont compile
(eight++).print(); // expected: 8
I'm not sure how to overload parenthesis.
I'm also not sure how this would work in this case:
(eight++)->print(); // expected: 8
In case eight is pointer.
Your ++ operators should return *this;
That should solve it.
For all numeric types, foo++ and ++foo HAVE a value. If you overload these operators without returning anything...it's misleading. Your overloads are limited, they can be used only if the calling code discards the value.
People expect that any piece of code written like:
number eight(8); ++eight; eight.print(); // prints 9
is 100% identical to:
number eight(8); (++eight).print(); // prints 9
With your class, the second form cannot be used at all (it just doesn't compile) because your overloads don't return a number.
(edit for clarity ) If you make your two overloads return a 'number' they will work both when the value is used, and when it's discarded.
I found an example at the bottom of the following page: http://www.cs.bu.edu/teaching/cpp/overload/incr-op.html
Clock Clock::operator++() // prefix form { tick(); return *this; } Clock::operator++(int) // postfix form { Clock c = *this; // Makes a backup of the current instance tick(); return c; // returns the value of the backup }
Actually no try this one.
Actually, no try this with your code :
Alright guys, tell me whats wrong with this one.
So when I use:
return *this;
that returns an instance, object, pointer to what exactly?
The address of the number class in memory, am I correct?
this is a pointer. *this is the object itself.
You didn't make those methods return a reference, you just made them return a copy of the object, which isn't that useful. There is a slight difference in the method prototypes they gave, and the ones you pasted.
number& number::operator++() { value++; }
This doesn't return anything.
Probably because you didn't return anything.
There is a slight difference in the method prototypes they gave, and the ones you pasted.
But this is the same thing I'm doing.
Their code.
Number ans = *this; ++(*this); // or just call operator++() return ans;
my code.
Except their example doesn't specify what exactly they are incrementing. I have a type int called value. I rewrote the first method for the prefix ++ operation.
You didn't make those methods return a reference, you just made them return a copy of the object, which isn't that useful.
Would this work?
number* ans = *this; return *ans;
Would this work?
no.
prefix increment and decrement should both return a number reference to *this.
postfix increment and decrement should both return a copy of *this made before this->value is incremented or decremented.
number reference to *this.
Can you show some code please.
copy of *this made before this->value
Similarly to what I did except I didn't do it in the correct order?
Can you show some code please.
Similarly to what I did except I didn't do it in the correct order?
Yes, the problem with yours was that you returned *this after incrementing this->value. That's not what postfix is supposed to do - it should give you the same value as the variable itself, and it can only do that if it returns a copy made before the incrementation takes place.
Thanks Edgar.
In the code:
number& number::operator--()
Is the ampersand used for passing the reference to *this?
Is the ampersand used for passing the reference to *this?
Yes.
The ampersand literally means and reads 'reference'. A reference is used like a variable, but behind the scenes works like a pointer, allowing you to work directly on a variable from the same or a different scope.
void alter_copy(int a) { a = 5;// duh, only altered a local copy of a } void alter_reference(int& a) { a = 5;// Ooh, we actually altered the variable that was passed to us. } void alter_by_pointer(int* pa) { *pa = 5;// we used the address of a int variable to alter its value // same as reference alteration above, but we had to dereference // the pointer passed to us. }
So when you return a reference, you are allowing the user to modify the variable you are returning to them, in this case, *this (the data pointed to by this). Fair warning - never return a reference to a stack object, because it goes out of scope and you are using deallocated memory.
You really should let him do his own research.
Yes.
The ampersand literally means and reads 'reference'. A reference is used like a variable, but behind the scenes works like a pointer, allowing you to work directly on a variable from the same or a different scope.
thanks for your help. Its everything I already knew but I wanted to be sure of.
(return type) (function name) (function arguments) { (process that includes arguments) return (return type); }