9.3.1/3 [class.mfct.non-static]
When an id-expression (5.1) that is not part of a class member access syntax (5.2.5) and not used to form a pointer to member (5.3.1) is used in a member of class X in a context where this can be used (5.1.1), if name lookup (3.4) resolves the name in the id-expression to a non-static non-type member of some class C, and if either the id-expression is potentially evaluated or C is X or a base class of X, the id-expression is transformed into a class member access expression (5.2.5) using (*this) (9.3.2) as the postfix-expression to the left of the . operator
Basically that tells that return _fName; is actually return (*this)._fName;
2011 Standard wrote:
9.3.1/4 A non-static member function may be declared const, volatile, or const volatile. These cv-qualifiers affect the type of the this pointer (9.3.2).
This tells that this becomes of type const Student*. When accessing a structure through pointer to const value, all its members are threated as const-qualified.
So your char _fName [20]; is actually constchar _fName [20]; when accessing through const this pointer (const member function). constchar[20] can be converted to constchar* per standard conversion rules. constchar* cannot be safely converted to return type of char* so there is an error.
Because int* is treated as int* const when accessed through const this. And you can copy a const object, you return a copy of that int*
Class contains pointer, not the array of values itself.
Effect of adding const and converting to pointer:
if so, why char _fName [20] becomes constchar _fName [20] (const is from the left), and on the other hand int* _id becomes int* const _id (const is from the right)?
BEcause one is an array and adding a const qualifier changes access to data (as in actual values in array). And second one is a pointer and adding const qualifier changess access to value (as in actual numeric value of pointer).
If it helps, you can write const after data type:
1 2
charconst fname[20];
int* const some_member;
Note that int* const is not intconst *, nor intconst * const and is not interchangable with them.
And second one is a pointer and adding const qualifier changess access to value
, so I don't understand why it's selective, in the array the const was automatically added from the left, when in 2nd case it was added actually from the right
They are added to value (data, content...). As I shown you can write const qualifier on the right of data type if you want.
In both cases semantics is the same. When you add const to array, you cannot change its content (a bunch of chars in your case). When you add const to ponter, you cannot change in content (a single number: an address). Nothing prevents from using pointer in different way without changing it (dereferenceing it to gain a reference to some outside memory area containing a bunch of ints)