Re: post increment not post

From: Robert Swan (rswan_at_sympatico.ca)
Date: 12/29/04


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
}



Relevant Pages

  • Re: GCC difference in size of long int on Suse SLES9 / Suse Professional 10.0
    ... The kernel uses printf() with 'long int', ... I can't compile 'on-the-fly'. ... clashing with the hardware and any libs that you interact with. ... platform types build up their own int types. ...
    (comp.os.linux.development.apps)
  • Re: Im a C++ programmer, and Relfs X.CPP is good.
    ... >> and it doesn't compile either. ... home.cpp:93: implicit declaration of function `int fclose' ... You cannot change to your home directory through an external ...
    (comp.lang.lisp)
  • Re: Im a C++ programmer, and Relfs X.CPP is good.
    ... >> and it doesn't compile either. ... home.cpp:93: implicit declaration of function `int fclose' ... You cannot change to your home directory through an external ...
    (comp.unix.programmer)
  • Re: non-static context
    ... public MyStatic(int length, int width) { ... Within the MyStatic constructor method, a local variable named area is ... This version of class MyStatic won't compile. ...
    (comp.lang.java.help)
  • Re: new order doubt
    ... Warning test2.c: 8 no type specified. ... Defaulting to int ... Tests probably come in several different types: this should compile ... Even Microsoft has problems with this obscure rules. ...
    (comp.lang.c)