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

前端 未结 8 835
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:13

    const(1) char const(2) * const GetName() { return m_name; } const(3);

    const char * const result = aaa.GetNAme();

    3 - const method, not allowed to change members nor call any non-const methods.

    1 - does not allow to modify inside the pointer, i.e. *result = ..

    2 - does not allow to move the pointer, i.e. result = NULL

    0 讨论(0)
  • 2020-12-19 09:17

    (1)const char (2)const * (3)const GetName() { return m_name; } (4)const;

    1. The contents of char array is const. This is good when you return pointer of the object member. Since you give pointer to your member for 3rd party, you want to prevent it to be changed from outside.
    2. This form is not used frequently and essentially same as (1)
    3. Our pointer to char array is const, so you can not change where the pointer points too.
    4. it qualifies the GetName() intself, meaning that the method thus not change the class it applied too. Thus it can be called for const object of this type only. This form typically used as GetName(...) const.

    As already mentioned in another answers the trick to "remember" it it read from right to left:

    • const T * - pointer to const T
    • T * const - const pointer to T
    0 讨论(0)
  • 2020-12-19 09:17

    last const:

    • Function does not change the privates of the class

    one after last const:

    • It's a constant pointer (i.e. the place it points to is constant)

    second const:

    • The function returns a const char (i.e. the content of the char is constant)

    First:

    • No idea?

    So to be complete: The function returns a constant pointer (always same location) to a constant char(always same content) and the function does not modify the state of the class.

    0 讨论(0)
  • 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?

    0 讨论(0)
  • 2020-12-19 09:20

    It is possible that you missed "*" symbol before second const keyword.

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

    So it means that the function returns constant pointer to constant pointer to constant character.

    0 讨论(0)
  • 2020-12-19 09:24

    Given:

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

    The first and second const are equivalent, but only one of them is allowed -- i.e. you can put const before or after the type (char in this case) but only one or the other, not both. Either, however, says that the characters pointed to by the pointer cannot be written to.

    The const after the '*' means the pointer returned by the function cannot itself be modified. This is rarely seen on a return type -- what you're returning is a value, which can't be modified in any case (it's normally just assigned to some variable). This can, however, be meaningful in other contexts.

    The third const is only allowed on a member function. It says that when this function is called, the this pointer that's received will be a T const * const rather than a T * const, so the member function can only modify static or mutable members of the object, and if it invokes other member functions, they must be const as well. There is, however, a caveat, that it can also cast away the const'ness, in which case it can modify what it sees fit (with the further caveat that if the object was originally defined as const, rather than just having a const pointer to a normal (non-const) object, the results will be undefined).

    0 讨论(0)
提交回复
热议问题