const function doesn't return char*

Pages: 12
I don't understand you people. I have a simple question:
get_fName() should return char*.
_fName is char*.
_fName is not modified inside the function.

Why doesn't it compile???
Last edited on
That should be better formulation of my question:

1
2
3
4
5
6
7
8
9
10
11
  class Student
{
	int _id;
	char _fName [20];
        char* get_fName() const;
}
// implementation
  char* Student::get_fName () const
{
	return _fName;
}


Why doesn't it compile???
The compiler says that returned type doesn't match the function type
Last edited on
Lets look what standard has to say:

2011 Standard wrote:
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 const char _fName [20]; when accessing through const this pointer (const member function).
const char[20] can be converted to const char* per standard conversion rules. const char* cannot be safely converted to return type of char* so there is an error.
Last edited on
WOW, finally. This was a great explanation. Thank you, MiiNiPaa!
Wait a second, so why the example with the int* worked ok?
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:
1
2
T[n] → const T[n] → const T* const
T* → T* const
Last edited on
Because in the int* case, the pointer was just a pointer. The data that it pointed to did not belong to the class in question.

For the char _fName[20], the array itself with all 20 elements, belonged to the class, so the class cared about who could touch it.
Last edited on
if so, why char _fName [20] becomes const char _fName [20] (const is from the left), and on the other hand int* _id becomes int* const _id (const is from the right)?
Last edited on
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
char const fname[20];
int* const some_member;


Note that int* const is not int const *, nor int const * 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)
OK, that was very helpful. Thank you people.
Topic archived. No new replies allowed.
Pages: 12