为什么类型字符的常量指针作为函数?

EEs*_*tud 0 c++

我正在学习C++而且我遇到过这个,我不明白这个小东西.为什么GetName()函数是字符类型的指针,为什么它是常量?

class Derived: public Base
{
public:
    Derived(int nValue)
        : Base(nValue)
    {
    }

    const char* GetName() { return "Derived"; }
    int GetValueDoubled() { return m_nValue * 2; }
};
Run Code Online (Sandbox Code Playgroud)

Luc*_*ore 8

"Derived"是一个字符串文字(查找).如果您尝试修改字符串文字,则会得到未定义的行为,因此会标记返回类型,const因此您不会意外地修改它.