Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Special member functions

From Wikipedia, the free encyclopedia
In C++, functions which the compiler will generate automatically if not declared

In theC++programming language,special member functions[1] arefunctions which thecompiler will automatically generate if they are used, but notdeclared explicitly by the programmer.The automatically generated special member functions are:

If a destructor is declared generation of a copy constructor is deprecated (C++11, proposal N3242[2]).
  • Move constructor if no copy constructor, copy assignment operator, move assignment operator and destructor are explicitly declared.
  • Copy assignment operator if no move constructor and move assignment operator are explicitly declared.
If a destructor is declared, generation of a copy assignment operator is deprecated.

In these cases the compiler generated versions of these functions perform amemberwise operation. For example, the compiler generated destructor will destroy each sub-object (base class or member) of the object.

The compiler generated functions will bepublic, non-virtual[3] and the copy constructor and assignment operators will receiveconst& parameters (and not be of thealternative legal forms).[4]

Example

[edit]

The following example depicts two classes:Explicit for which all special member functions are explicitly declared andImplicit for which none are declared.

#include<iostream>#include<string>#include<utility>classExplicit{public:Explicit(){std::cout<<"Default constructor "<<message_<<'\n';}explicitExplicit(std::stringmessage):message_(std::move(message)){std::cout<<"Non-default constructor "<<message_<<'\n';}Explicit(constExplicit&other){std::cout<<"Copy constructor "<<message_<<'\n';*this=other;// invoke copy assignment operator}Explicit&operator=(constExplicit&other){std::cout<<"Copy assignment operator "<<message_<<'\n';if(this!=&other){message_=other.message_;}return*this;}Explicit(Explicit&&other)noexcept{std::cout<<"Move constructor "<<message_<<'\n';*this=std::move(other);// invoke move assignment operator}Explicit&operator=(Explicit&&other)noexcept{std::cout<<"Move assignment operator "<<message_<<'\n';if(this!=&other){message_=std::move(other.message_);}return*this;}~Explicit(){std::cout<<"Destructor "<<message_<<'\n';}private:friendclassImplicit;std::stringmessage_;};classImplicit:publicExplicit{public:voidSpew(){std::cout<<"Implicit("<<message_<<", "<<member_.message_<<")\n";}private:Explicitmember_;};

Signatures

[edit]

Here are the signatures of the special member functions:

Functionsyntax for class MyClass
Default constructorMyClass();
Copy constructorMyClass(const MyClass& other);
Move constructorMyClass(MyClass&& other) noexcept;
Copy assignment operatorMyClass& operator=(const MyClass& other);
Move assignment operatorMyClass& operator=(MyClass&& other) noexcept;
Destructorvirtual ~MyClass();

C++03

[edit]

In C++03 before the introduction ofmove semantics (in C++11) the special member functions[5] were:

References

[edit]
  1. ^ISO/IEC (2011).ISO/IEC 14882:2011 (3 ed.). ISO/IEC. pp. §12.
  2. ^"Enforcing the Rule of Zero".
  3. ^Except for the destructor if a base class already has a virtual destructor.
  4. ^Similarly, the move constructor/assignment operators will receive&& parameters instead of the alternatives.
  5. ^ISO/IEC (1998).International Standard ISO/IEC 14882: Programming languages—C++ = Languages de programmation—C++ (1 ed.). ISO/IEC. pp. §12.OCLC 71718919.
Features
Standard Library
Ideas
Compilers
IDEs
Superset languages
Dialects
Relative to
other languages
People
Retrieved from "https://en.wikipedia.org/w/index.php?title=Special_member_functions&oldid=1209452723"
Category:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp