cpp/language/member functions

A non-static member function is a function that is declared in a of a class without a  or  specifier (see and  for the effect of those keywords).

, s, and use special syntaxes for their declarations. The rules described in this page may not apply to these functions. See their respective pages for details.

An implicit object member function is a non-static member function without an explicit object parameter (prior to C++23, this was the only kind of non-static member function, and hence referred to as "non-static member function" in the literature).

Explanation
Any are allowed, with additional syntax elements that are only available for non-static member functions:, cv-qualifiers, and.

A non-static member function of class may be called @1@ For an object of type using the class member access operator @2@ For an object of a class from @3@ Directly from within the body of a member function of @4@ Directly from within the body of a member function of a class derived from

Calling a non-static member function of class on an object that is not of type, or of a type derived from  invokes undefined behavior.

Within the body of a non-static member function of, any  (e.g. an identifier) that resolves to a non-type non-static member of  or of a base class of , is transformed to a member access expression  (unless it's already a part of a member access expression). This does not occur in template definition context, so a name may have to be prefixed with explicitly to become.

Within the body of a non-static member function of, any unqualified-id that resolves to a static member, an enumerator or a nested type of or of a base class of , is transformed to the corresponding qualified-id:

Member functions with cv-qualifiers
An implicit object member function can be declared with a sequence (,, or a combination of  and ), this sequence appears after the parameter list in the. Functions with different cv-qualifier sequences (or no sequence) have different types and so may overload each other.

In the body of a function with a cv-qualifier sequence, is cv-qualified, e.g. in a member function with  qualifier, only other member functions with  qualifier may be called normally. A member function without qualifier may still be called if  is applied or through an access path that does not involve.

{{rrev|since=c++11|

Member functions with ref-qualifier
An implicit object member function can be declared with no ref-qualifier, with an lvalue ref-qualifier (the token after the parameter list) or the rvalue ref-qualifier (the token  after the parameter list). During, an implicit object member function with a cv-qualifier sequence of class X is treated as follows:
 * no ref-qualifier: the implicit object parameter has type lvalue reference to cv-qualified X and is additionally allowed to bind rvalue implied object argument
 * lvalue ref-qualifier: the implicit object parameter has type lvalue reference to cv-qualified X
 * rvalue ref-qualifier: the implicit object parameter has type rvalue reference to cv-qualified X

Note: unlike cv-qualification, ref-qualification does not change the properties of the pointer: within an rvalue ref-qualified function,  remains an lvalue expression. }}

Virtual and pure virtual functions
A non-static member function may be declared virtual or pure virtual. See and  for details.

{{rrev|since=c++23|

Explicit object parameter
A non-static member function can be declared to take as its first parameter an explicit object parameter, denoted with the prefixed keyword.

For template member functions, explicit object parameter allows deduction of type and value category, this language feature is called "deducing ".

This makes it possible to deduplicate const- and non-const member functions, see array subscript operator for an example.

Additionally, explicit object parameter deduces to the derived type, which simplifies :

Inside the body of a function with explicit object parameter, the pointer cannot be used: all member access must be done through the first parameter, like in static member functions:

A pointer to a member function with explicit object parameter is an ordinary pointer to function, not a pointer to member:

Member functions with an explicit object parameter cannot be static or virtual and they cannot have cv- or ref-qualifiers. }}

Special member functions
Some member functions are special: under certain circumstances they are defined by the compiler even if not defined by the user. They are:





Special member functions are the only functions that can be defaulted, that is, defined using  instead of the function body (see their pages for details).