operator << : std::cout << i << (i << 1);

Posted by Oops on Stack Overflow See other posts from Stack Overflow or by Oops
Published on 2010-04-21T21:13:28Z Indexed on 2010/04/21 21:23 UTC
Read the original article Hit count: 152

Filed under:

Hi,
I use the stream operator << and the bit shifting operator << in one line. I am a bit confused, why does code A) not produce the same output than code B)?

A)

int i = 4;  
std::cout << i << " " << (i << 1) << std::endl;   //4 8

B)

myint m = 4;
std::cout << m << " " << (m << 1) << std::endl;   //8 8

class myint:

class myint {
    int i;
public:
    myint(int ii) {
        i = ii;
    }
    inline myint operator <<(int n){
        i = i << n;
        return *this;
    }
    inline operator int(){
        return i;
    }
};

thanks in advance
Oops

© Stack Overflow or respective owner

Related posts about c++