11 Classes [class]

11.4 Class members [class.mem]

11.4.2 Non-static member functions [class.mfct.non-static]

A non-static member function may be called for an object of its class type, or for an object of a class derived from its class type, using the class member access syntax ([over.match.call]).
A non-static member function may also be called directly using the function call syntax ([expr.call], [over.match.call]) from within its class or a class derived from its class, or a member thereof, as described below.
If a non-static member function of a class X is called for an object that is not of type X, or of a type derived from X, the behavior is undefined.
When an id-expression that is not part of a class member access syntax and not used to form a pointer to member ([expr.unary.op]) is used in a member of class X in a context where this can be used, if name lookup 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 using (*this) as the postfix-expression to the left of the . operator.
Note
:
If C is not X or a base class of X, the class member access expression is ill-formed.
— end note
 ]
This transformation does not apply in the template definition context ([temp.dep.type]).
Example
:
struct tnode {
  char tword[20];
  int count;
  tnode* left;
  tnode* right;
  void set(const char*, tnode* l, tnode* r);
};

void tnode::set(const char* w, tnode* l, tnode* r) {
  count = strlen(w)+1;
  if (sizeof(tword)<=count)
      perror("tnode string too long");
  strcpy(tword,w);
  left = l;
  right = r;
}

void f(tnode n1, tnode n2) {
  n1.set("abc",&n2,0);
  n2.set("def",0,0);
}
In the body of the member function tnode​::​set, the member names tword, count, left, and right refer to members of the object for which the function is called.
Thus, in the call n1.set("abc",&n2,0), tword refers to n1.tword, and in the call n2.set("def",0,0), it refers to n2.tword.
The functions strlen, perror, and strcpy are not members of the class tnode and should be declared elsewhere.102
— end example
 ]
A non-static member function may be declared const, volatile, or const volatile.
These cv-qualifiers affect the type of the this pointer.
They also affect the function type of the member function; a member function declared const is a const member function, a member function declared volatile is a volatile member function and a member function declared const volatile is a const volatile member function.
Example
:
struct X {
  void g() const;
  void h() const volatile;
};
X​::​g is a const member function and X​::​h is a const volatile member function.
— end example
 ]
A non-static member function may be declared with a ref-qualifier ([dcl.fct]); see [over.match.funcs].
A non-static member function may be declared virtual ([class.virtual]) or pure virtual ([class.abstract]).
See, for example, <cstring> ([cstring.syn]).

11.4.2.1 The this pointer [class.this]

In the body of a non-static ([class.mfct]) member function, the keyword this is a prvalue whose value is a pointer to the object for which the function is called.
The type of this in a member function whose type has a cv-qualifier-seq cv and whose class is X is “pointer to cv X.
Note
:
Thus in a const member function, the object for which the function is called is accessed through a const access path.
— end note
 ]
Example
:
struct s {
  int a;
  int f() const;
  int g() { return a++; }
  int h() const { return a++; } // error
};

int s::f() const { return a; }
The a++ in the body of s​::​h is ill-formed because it tries to modify (a part of) the object for which s​::​h() is called.
This is not allowed in a const member function because this is a pointer to const; that is, *this has const type.
— end example
 ]
Note
:
Similarly, volatile semantics apply in volatile member functions when accessing the object and its non-static data members.
— end note
 ]
A member function whose type has a cv-qualifier-seq cv1 can be called on an object expression of type cv2 T only if cv1 is the same as or more cv-qualified than cv2 ([basic.type.qualifier]).
Example
:
void k(s& x, const s& y) {
  x.f();
  x.g();
  y.f();
  y.g();                        // error
}
The call y.g() is ill-formed because y is const and s​::​g() is a non-const member function, that is, s​::​g() is less-qualified than the object expression y.
— end example
 ]
Note
:
Constructors and destructors cannot be declared const, volatile, or const volatile.
However, these functions can be invoked to create and destroy objects with cv-qualified types; see [class.ctor] and [class.dtor].
— end note
 ]