I’m learning C++ and I’ve came across this and I don’t understand this small thing. Why is the GetName() function a pointer of type character and why is it constant?
class Derived: public Base
{
public:
Derived(int nValue)
: Base(nValue)
{
}
const char* GetName() { return "Derived"; }
int GetValueDoubled() { return m_nValue * 2; }
};
"Derived"is a string literal (look it up). If you attempt to modify a string literal, you get undefined behaviour, so the return type is markedconstso you don’t accidentally modify it.