问题Const correctness: const char const * const GetName const (parameter) {some code;};

问:const char const * const GetName() const { return m_name; };
So, what is the explanation for each of these consts?

答: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.


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.

posted @ 2012-07-15 14:03  simplefrog  阅读(303)  评论(0编辑  收藏  举报