Const correctness: const char const * const GetName const (//stuff);

前端 未结 8 842
Happy的楠姐
Happy的楠姐 2020-12-19 08:48

Labelled as homework because this was a question on a midterm I wrote that I don\'t understand the answer to. I was asked to explain the purpose of each const in the follow

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-19 09:28

    You have one more const than is syntactically allowed, that code would not compile. Remove the "const" after "char" and before the "*". Also, the last const must come before the function body. It helps to read things like this from right to left.

    const char * const GetName() const { return m_name; };
    

    You have a const function (i.e., the function does not alter the state of the class.), which returns a const pointer to a const char.

提交回复
热议问题