Problem with an overloaded cast operator

Ryszard Kabatek rumcajs@gmx.net
Mon Apr 3 00:15:00 GMT 2000


I wolud expect, the sample below should not compile (g++ -Wall), 
because the function f() returns X by value and the copying of X 
is forbidden.

The most interesting is the last line in main, where I make 
an explicite call of an operator from an object that cannot exist.

I supposed a bug in the g++ but I got the same results with HP's aCC.
In any case there is a problem with the overloaded cast operator.
Any suggestions?

Ryszard Kabatek


               # include <iostream>

               class X {
                   friend X f();
                 public:
                   operator const char* () const {return p;}
                   ~X() {}
                 private:
                   X(const X&);
                   X& operator=(const X&);
                   X(char* ptr) : p(ptr) {}
                   char* p;
               };

               X f() {return X(0);}

               int main()
               {
                 const char* p1 = f();
                 const char* p2 = f().operator const char*();
               }

-- 
Sent through GMX FreeMail - http://www.gmx.net



More information about the Gcc mailing list