Overloading by return type

前端 未结 11 1748
我在风中等你
我在风中等你 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条回答
  •  旧时难觅i
    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.

提交回复
热议问题