C++ same function parameters with different return type

前端 未结 10 1096
猫巷女王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:53

    Whether it is an overload or a specialization, you'll need the information to be in the function signature. You could pass the variable in as an unused 2nd argument:

    int RetrieveValue(const std::string& s, const int&) {
      return atoi(s.c_str());
    }
    double RetrieveValue(const std::string& s, const double&) {
      return atof(s.c_str());
    }
    
    int i = RetrieveValue(dataString1, i);
    double d = RetrieveValue(dataString2, d);
    

提交回复
热议问题