proxy - how does operator overload resolution work based on return type in the following code of c++ -


i know there no legal overload based on return type in c++; i.e. cannot like:

int operator ++ getowner(); char operator ++ getowner(); 

however, stumbled upon following:

https://stackoverflow.com/a/9569120/1376317

class proxy {     const* myowner; public:     proxy( const* owner ) : myowner( owner ) {}     operator int() const     {         return myowner->getint();     }     operator char() const     {         return myowner->getchar();     } }; 

my question how operator overload work in configuration. how call in main.cpp kind of overloading. how compiler deduce , how call right overload?

my question how operator overload work in configuration.

these operators provide implicit conversions. means class can used in many contexts int or char expected, , use these operators provide expected value.

how call in main.cpp kind of overloading.

here few examples of implicit conversions:

proxy p = whatever(); int = p;   // convert int char c = p;  // convert char long l = p;  // error: ambiguous  void f(int); f(p);        // convert int  void g(int); void g(char); g(p);        // error: ambiguous 

you can request explicit conversions using usual cast notations:

long l = static_cast<int>(p);  // convert int, long g((char)p);                    // convert char 

how compiler deduce , how call right overload?

whenever there's type mismatch, compiler looks conversion sequence. rules quite complicated, sequence can include @ 1 user-defined conversion (using either operator this, or converting construction), standard conversions such int long.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

mysql - Pass value between different pages (form) with PHP -