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) Show
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
[edit] ExplanationAny 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-qualifiersA 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
[edit] Virtual and pure virtual functionsA non-static member function may be declared virtual or pure virtual. See virtual functions and abstract classes for details.
[edit] Special member functionsSome 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 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
[edit] Example#include Output: ctor1 called, data = 1 ctor2 called, data = 2 ctor2 failed, string was 'not a number' 2 ctor1 called, data = 3 [edit] Defect reportsThe following behavior-changing defect reports were applied retroactively to previously published C++ standards.
[edit] See also
Which of the following is a special member function that is executed when an object of its class goes out of scope?A - A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope.
Which of the following gets called when an object goes out of scope Mcq?A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to delete.
Which of the functions can only be called by another function that is a member of its class in C++?A private member function can be called by another function that is a member of a class iscalled.
Which of the functions can only be called by another function that is a member of its select only one option class?member function can only be called by another function that is member of its class.
|