I have troubles with a C++ class constructor, using GCC.
The class "foo", hereunder, is supposed to emulate a processor register like AL, AH, AX, EAX,... and i need to have some basic arithmetic associated with this class. but i have a strange behaviour in the initialisation or a "foo" object.
I don't have have the same result for the 2 following cases:
foo w=0x12345678; // case 1 foo w ; // case 2 init ( 2 steps) w=0x12345678;
For me, case 2 is working GCC calls foo() ( constructor 1) then the = operator. At the end, w.m_val is ok But for case 1 GCC calls directly foo( long *) ( constructor 2) and nothing else. Obviously that's not what i'm expecting.
If "foo" where char , int or long, the result would be the same for both cases.
I maybe misunderstood something about constructors or did something wrong. Could somebody help me ?
Thanks.
class foo { public: foo(){ // constructor 1 m_val=0; m_ptr=NULL; }; foo(long *p){ // constructor 2, should never be called!!! m_val=0; m_ptr=p; }; friend foo operator+( const foo &rhs, const unsigned int v ); foo &operator+= (unsigned int v ) { m_val+=v; return *this; } ~foo(){}; foo &operator= ( const foo &rhs ) { m_val=rhs.m_val; return *this; }; foo &operator= ( const unsigned int v ) { m_val=v; return *this; }; private: unsigned int m_val; long *m_ptr; };
foo w=0x12345678;
which is what I would expect. So either you have found a compiler bug (unlikely but not impossible) or you haven't posted your real code. – john