Constant: const char const * const GetName const (// material);

Marked as homework because it was a medium-term question, I wrote that I did not understand the answer. I was asked to explain the purpose of each const in the following statement:

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

So what is the explanation for each of these consts?

+6
c ++
source share
8 answers

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

Ok, now the return value:

 const char const *const 

This is a constant pointer to everything ... here we start the boom! You have an extra const syntax error. The following equivalents are: const T or T const . If you select const , you will get a constant pointer to constant characters. Does it help?

+6
source share

You have another const that is syntactically resolved, this code will not compile. Remove "const" after "char" and before "*". In addition, the last constant must be in front of the function body. It helps to read such things from right to left.

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

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

+2
source share

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

  • The contents of the char array are constant. This is good when you return a pointer to an object. Since you are pointing to a member’s pointer for a third-party member, you want it to not be editable from the outside.
  • This form is not used often and is essentially the same as (1)
  • Our pointer to a char array is constant, so you cannot change where the pointer points too.
  • it qualifies the GetName () property, which means that the method in this way does not change the class that it used. Thus, it can only be called for a const object of this type. This form is commonly used as GetName (...) const.

As mentioned in another answer to the trick, in order to “remember” it, it is read from right to left:

  • const T * - pointer to const T
  • T * const - const pointer to T
+2
source share

Edit: It looks like I incorrectly inserted the code in Como, or it was edited in the original answer to be correct. In any case, I keep the answer below as if the code was incorrect.

The Comeau online compiler produces the following results:

"ComeauTest.c", line 4: error: enter the qualifier specified more than once
const char const * const GetName () {return m_name; } const; ^

"ComeauTest.c", line 4: warning: enter a qualifier by return type meaningless const char const * const GetName () {return m_name; } const; ^

"ComeauTest.c", line 4: error: declaration declares nothing const char const * const GetName () {return m_name; } const;

This means that your statement is incorrect.

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

The first and second constants mean the same thing. You cannot specify the same qualifier more than once, so that one of them had to be removed to compile the code. Both of these constants indicate that the values ​​pointed to by the pointer returned by GetName cannot be changed, which makes such code invalid:

 const char* name = c.GetName(); name[0] = 'a'; 

The third const indicates that the pointer returned by GetName () itself cannot be changed, but, as Como points out, it does nothing on the return value, since the return value is a copy of the pointer, not the pointer, and can be assigned to a non-continent pointer.

The fourth constant is inappropriate; it should be between GetName and the function body as follows:

 const char* GetName() const { return m.name; } 

This constant indicates that no members of the class will be changed during GetName. Assuming GetName is a member of the Person class, this code will be resolved:

 const Person& p; p.GetName(); 

Without this constant, the above code will not work.

+1
source share

You may have missed the "*" before the const keyword.

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

Thus, this means that the function returns a constant pointer to a constant pointer to a constant character.

+1
source share

last const:

  • The function does not change the ordinary members of the class

one after the last const:

  • This is a constant pointer (i.e. the place it points to is constant)

second constant:

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

At first:

  • I do not know?

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

0
source share

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

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

3 - const method, it is not allowed to change members and not to name any non-constant methods.

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

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

0
source share

Given:

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

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

const after the '*' symbol means that the pointer returned by the function itself cannot be changed. This is rare in the return type - what you return is a value that cannot be changed in any case (it is usually just assigned to some variable). However, this may make sense in other contexts.

The third const is only allowed for a member function. It says that when this function is called, the this pointer that was received will be T const * const , not T * const , so a member function can only change the members of a static or mutable object, and if it calls other member functions, they must also be const . There is, however, a caveat that it can also discard const'ness, in which case it can change what it considers necessary (with a further warning that if the object was originally defined as const , and not just a const pointer to ordinary (non-constant) object, the results will be undefined).

0
source share

All Articles