C++ same function parameters with different return type

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

    Unfortunately there is no way to overload the function return type see this answer Overloading by return type

    0 讨论(0)
  • 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;
        }
    }
    
    0 讨论(0)
  • 2020-12-16 18:50

    As an alternative to the template solution, you can have the function return a reference or a pointer to a class, then create subclasses of that class to contain the different data types that you'd like to return. RetrieveValue would then return a reference to the appropriate subclass.

    That would then let the user pass the returned object to other functions without knowing which subclass it belonged to.

    The problem in this case would then become one of memory management -- choosing which function allocates the returned object and which function deletes it, and when, in such a way that we avoid memory leaks.

    0 讨论(0)
  • 2020-12-16 18:52
    int a=itoa(retrieveValue(dataString));
    double a=ftoa(retrieveValue(dataString));
    

    both return a string.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-16 19:01

    You've to start with this:

    template<typename T>
    T RetrieveValue(std::string key)
    {
         //get value and convert into T and return it
    }
    

    To support this function, you've to work a bit more, in order to convert the value into the type T. One easy way to convert value could be this:

    template<typename T>
    T RetrieveValue(std::string key)
    {
         //get value
          std::string value = get_value(key, etc);
    
          std::stringstream ss(value);
          T convertedValue;
          if ( ss >> convertedValue ) return convertedValue;
          else throw std::runtime_error("conversion failed");
    }
    

    Note that you still have to call this function as:

    int x = RetrieveValue<int>(key);
    

    You could avoid mentioning int twice, if you could do this instead:

    Value RetrieveValue(std::string key)
    {
         //get value
          std::string value = get_value(key, etc);
          return { value };
    }
    

    where Value is implemented as:

    struct Value
    {
        std::string _value;
    
        template<typename T>
        operator T() const   //implicitly convert into T
        {
           std::stringstream ss(_value);
           T convertedValue;
           if ( ss >> convertedValue ) return convertedValue;
           else throw std::runtime_error("conversion failed");
        }
    }
    

    Then you could write this:

    int    x = RetrieveValue(key1);
    double y = RetrieveValue(key2);
    

    which is which you want, right?

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