Which of the following is another special member function that is called when the scope of the object ends?

A non-static member function is a function that is declared in a member specification of a class without a static or friend specifier. [see static member functions and friend declaration for the effect of those keywords]

class S { int mf1[]; // non-static member function declaration void mf2[] volatile, mf3[] &&; // can have cv-qualifiers and/or a reference-qualifier // the declaration above is equivalent to two separate declarations: // void mf2[] volatile; // void mf3[] &&;   int mf4[] const { return data; } // can be defined inline virtual void mf5[] final; // can be virtual, can use final/override S[] : data[12] {} // constructors are member functions too int data; };   int S::mf1[] { return 7; } // if not defined inline, has to be defined at namespace

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

Contents

  • 1 Explanation
    • 1.1 member functions with cv-qualifiers
    • 1.2 member functions with ref-qualifier
    • 1.3 Virtual and pure virtual functions
    • 1.4 Explicit object parameter
    • 1.5 Special member functions
  • 2 Notes
  • 3 Example
  • 4 Defect reports
  • 5 See also

[edit] Explanation

Any function declarations are allowed, with additional syntax elements that are only available for non-static member functions: pure-specifiers, cv-qualifiers, ref-qualifiers, final and override specifiers [since C++11], and member initialization lists.

A non-static member function of class X may be called

1] For an object of type X using the class member access operator

2] For an object of a class derived from X

3] Directly from within the body of a member function of X

4] Directly from within the body of a member function of a class derived from X

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

Within the body of a non-static member function of X, any id-expression e [e.g. an identifier] that resolves to a non-type non-static member of X or of a base class of X, is transformed to a member access expression [*this].e [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 this-> explicitly to become dependent.

struct S { int n; void f[]; };   void S::f[] { n = 1; // transformed to [*this].n = 1; }   int main[] { S s1, s2; s1.f[]; // changes s1.n }

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

struct S { static int n; void f[]; };   void S::f[] { n = 1; // transformed to S::n = 1; }   int main[] { S s1, s2; s1.f[]; // changes S::n }

[edit] member functions with cv-qualifiers

A non-static member function can be declared with a cv-qualifier sequence [const, volatile, or a combination of const and volatile], this sequence appears after the parameter list in the function declaration. 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, *this is cv-qualified, e.g. in a member function with const qualifier, only other member functions with const qualifier may be called normally. [A member function without const qualifier may still be called if const_cast is applied or through an access path that does not involve this.]

#include   struct Array { std::vector data; Array[int sz] : data[sz] {}   // const member function int operator[][int idx] const { // the this pointer has type const Array* return data[idx]; // transformed to [*this].data[idx]; }   // non-const member function int& operator[][int idx] { // the this pointer has type Array* return data[idx]; // transformed to [*this].data[idx] } };   int main[] { Array a[10]; a[1] = 1; // OK: the type of a[1] is int& const Array ca[10]; ca[1] = 2; // Error: the type of ca[1] is int }

member functions with ref-qualifier

A non-static 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 overload resolution, non-static 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

#include   struct S { void f[] & { std::cout c.bar[]; // ok } };

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

struct Y { int f[int, int] const&; int g[this Y const&, int, int]; };   auto pf = &Y::f; pf[y, 1, 2]; // error: pointers to member functions are not callable [y.*pf][1, 2]; // ok std::invoke[pf, y, 1, 2]; // ok   auto pg = &Y::g; pg[y, 3, 4]; // ok [y.*pg][3, 4]; // error: pg is not a pointer to member function std::invoke[pg, y, 3, 4]; // ok


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

[since C++23]

[edit] 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:

  • Default constructor
  • Copy constructor
  • Copy assignment operator
  • Destructor [until C++20]Prospective destructor [since C++20]

Special member functions along with the comparison operators [since C++20] are the only functions that can be defaulted, that is, defined using = default instead of the function body [see their pages for details].

[edit] Notes

Feature-test macroValueStdComment
__cpp_ref_qualifiers 200710L [C++11] ref-qualifiers
__cpp_explicit_this_parameter 202110L [C++23] Explicit object parameter

[edit] Example

#include #include #include #include   struct S { int data;   // simple converting constructor [declaration] S[int val];   // simple explicit constructor [declaration] explicit S[std::string str];   // const member function [definition] virtual int getData[] const { return data; } };   // definition of the constructor S::S[int val] : data[val] { std::cout

Chủ Đề