Overloading by return type

前端 未结 11 1728
我在风中等你
我在风中等你 2020-11-22 07:07

I read few questions here on SO about this topic which seems yet confusing to me. I\'ve just begun to learn C++ and I haven\'t studied templates yet or operator overloading

相关标签:
11条回答
  • 2020-11-22 07:49

    you can't overload a function based on the return type of the function. you can overlead based on the type and number of arguments that this function takes.

    0 讨论(0)
  • 2020-11-22 07:54

    While most of the other comments on this problem are technically correct, you can effectively overload the return value if you combine it with overloading input parameter. For example:

    class My {
    public:
        int  get(int);
        char get(unsigned int);
    };
    

    DEMO:

    #include <stdio.h>
    
    class My {
    public:
        int  get(         int x) { return 'I';  };
        char get(unsinged int x) { return 'C';  };
    };
    
    int main() {
    
        int i;
        My test;
    
        printf( "%c\n", test.get(               i) );
        printf( "%c\n", test.get((unsigned int) i) );
    }
    

    The resulting out of this is:

    I 
    C
    
    0 讨论(0)
  • 2020-11-22 07:56

    No there isn't. You can't overload methods based on return type.

    Overload resolution takes into account the function signature. A function signature is made up of:

    • function name
    • cv-qualifiers
    • parameter types

    And here's the quote:

    1.3.11 signature

    the information about a function that participates in overload resolution (13.3): its parameter-type-list (8.3.5) and, if the function is a class member, the cv-qualifiers (if any) on the function itself and the class in which the member function is declared. [...]

    Options:

    1) change the method name:

    class My {
    public:
        int getInt(int);
        char getChar(int);
    };
    

    2) out parameter:

    class My {
    public:
        void get(int, int&);
        void get(int, char&);
    }
    

    3) templates... overkill in this case.

    0 讨论(0)
  • 2020-11-22 07:59

    It's possible, but I'm not sure that it's a technique I'd recommend for beginners. As in other cases, when you want the choice of functions to depend on how the return value is used, you use a proxy; first define functions like getChar and getInt, then a generic get() which returns a Proxy like this:

    class Proxy
    {
        My const* myOwner;
    public:
        Proxy( My const* owner ) : myOwner( owner ) {}
        operator int() const
        {
            return myOwner->getInt();
        }
        operator char() const
        {
            return myOwner->getChar();
        }
    };
    

    Extend it to as many types as you need.

    0 讨论(0)
  • 2020-11-22 08:00

    I used James Kanze's answer using a proxy:

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

    I wanted to avoid using lots of ugly static_casts on a void*, so I did this:

    #include <SDL_joystick.h>
    #include <SDL_gamecontroller.h>
    
    struct JoyDev {
        private:
            union {
                SDL_GameController* dev_gc = nullptr;
                SDL_Joystick*       dev_js;
            };
        public:
            operator SDL_GameController*&() { return dev_gc; }
            operator SDL_Joystick*&()       { return dev_js; }
    
            SDL_GameController*& operator=(SDL_GameController* p) { dev_gc = p; return dev_gc; }
            SDL_Joystick*&       operator=(SDL_Joystick* p)       { dev_js = p; return dev_js; }
    };
    
    struct JoyState {
        public:
            JoyDev dev;
    };
    
    int main(int argc, char** argv)
    {
        JoyState js;
    
        js.dev = SDL_JoystickOpen(0);
    
        js.dev = SDL_GameControllerOpen(0);
    
        SDL_GameControllerRumble(js.dev, 0xFFFF, 0xFFFF, 300);
    
        return 0;
    }
    

    Works perfectly!

    0 讨论(0)
提交回复
热议问题