How to overload operators with a built-in return type?

后端 未结 4 823
时光说笑
时光说笑 2021-01-23 06:56

Say I have a class, that wraps some mathematic operation. Lets use a toy example

class Test
{
public:
   Test( float f ) : mFloat( f ), mIsInt( false ) {}

   fl         


        
4条回答
  •  孤城傲影
    2021-01-23 07:35

    Using template specialization:

    class Config {
        template
        void setValue(const std::string& index, T& value); //sets the value if available
    };
    
    template
    void Config::setValue(const std::string& index, float& value){...} //only sets float values
    
    template
    void Config::setValue(const std::string& index, int& value){...} //only sets int values;
    

提交回复
热议问题