Re: post increment not post
From: Robert Swan (rswan_at_sympatico.ca)
Date: 12/29/04
- Next message: Victor Bazarov: "Re: post increment not post"
- Previous message: Ron Natalie: "Re: post increment not post"
- In reply to: Victor Bazarov: "Re: post increment not post"
- Next in thread: Victor Bazarov: "Re: post increment not post"
- Reply: Victor Bazarov: "Re: post increment not post"
- Reply: Andrey Tarasevich: "Re: post increment not post"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 29 Dec 2004 13:02:07 -0500
Victor Bazarov wrote:
...
>
> I am surprised it outputs anything. It's not supposed to compile.
It did. However, if I had enabled warnings the compiler would have
revealed the inept code.
>
>>
>> #include <iostream>
>>
>> class a {
>> int v;
>> public:
>> a():v(0){}
>> a& operator++(int) {
>> v++;
>
>
> This function is defined to have no 'return' statement although one
> is required for non-void function which is not a c-tor or d-tor.
> It is unknown what you _intended_ to do. The program is ill-formed.
Oops. I intended to define class a's post operator ++ to increment
member v, then return a reference to the "invokee object" (not sure what
you're supposed to call it).
I was then thinking the statement
int b = aa++;
should be equivalent to
b = aa; a++;
Seems that it's not.
#include <iostream>
class a {
int v;
public:
a():v(0){}
a& operator++(int) {
v++;
return *this;
}
operator int&() {
return v;
}
};
int main() {
a aa;
// I intend to initialize b with 0, then increment a
int b = aa++;
std::cout << b << std::endl;
// didn't happen
}
- Next message: Victor Bazarov: "Re: post increment not post"
- Previous message: Ron Natalie: "Re: post increment not post"
- In reply to: Victor Bazarov: "Re: post increment not post"
- Next in thread: Victor Bazarov: "Re: post increment not post"
- Reply: Victor Bazarov: "Re: post increment not post"
- Reply: Andrey Tarasevich: "Re: post increment not post"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|