简体   繁体   English

无法使用const char重载std :: string const和函数*

[英]Cannot overload std::string const & function with const char *

so I've got this: 所以我有这个:

class SettingBase {
public:
    virtual ~SettingBase() { }
};

template<typename T>
class Setting : private SettingBase {
    friend class Section;
public:
    std::string const &get_name() { return name; } // and my compiler
    const char *get_name() { return name.c_str(); } // doesn't like this.

    T const &get_value() throw() { return value; }

private:
    Setting(std::string name, T value) : name(name), value(value) { }
    Setting(const char *name, T value) : name(std::string(name)), value(value) { }

    std::string name;
    T value;
};

and then gcc says 然后gcc说

config.cpp:74:17: error: ‘const char* Setting<T>::get_name()’ cannot be overloaded
config.cpp:73:24: error: with ‘const std::string& Setting<T>::get_name()’

Any particular reason for this? 有什么特殊原因吗?

Thanks, 谢谢,

Julian. 朱利安。

You cannot overload solely based on return type because the return type is not considered during overload resolution. 您不能仅基于返回类型进行重载,因为在重载解析期间不会考虑返回类型。

Two overloads must have different parameter types, different numbers of parameters, or (for member functions) different const-, volatile-, and reference-qualification. 两个重载必须具有不同的参数类型,不同数量的参数或(对于成员函数而言)不同的const,volatile和引用限定。

因为返回类型不是函数重载的条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM