C++ same function parameters with different return type

前端 未结 10 1085
猫巷女王i
猫巷女王i 2020-12-16 18:25

I need to find some way to mock an overload of a function return type in C++.

I know that there isn\'t a way to do that directly, but I\'m hoping there\'s some out-o

10条回答
  •  生来不讨喜
    2020-12-16 18:49

    The answer is simple just declare the function returning void* type and in the definition return a reference to the variable of different types. For instance in the header (.h) declare

    void* RetrieveValue(string dataString1);
    

    And in the definition (.cpp) just write

    void* RetrieveValue(string dataString1)
    {
        if(dataString1.size()>4)
        {
            double value1=(double)dataString1.size();
            return &value1;
        }
        else
        {
            string value2=dataString1+"some string";
            return &value2;
        }
    }
    

提交回复
热议问题