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

前端 未结 8 852
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:19

    Take them from the right. The one before the ; tells the client this is a design level const i.e. it does not alter the state of the object. (Think of this as a read-only method.)

    Okay, now the return value:

    const char const *const
    

    This is a constant pointer to okay ... here we go boom! You have an extra const -- a syntax error. The following are equivalent: const T or T const. If you take out a const you get a constant pointer to a constant characters. Does that help?

提交回复
热议问题