C++ 编译器为类创建的所有成员函数是什么?是不是一直都这样?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3734247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:35:33  来源:igfitidea点击:

What are all the member-functions created by compiler for a class? Does that happen all the time?

c++member-functionsc++-faq

提问by Onnesh

What are all the member-functions created by compiler for a class? Does that happen all the time? like destructor. My concern is whether it is created for all the classes, and why is default constructor needed?

编译器为类创建的所有成员函数是什么?是不是一直都这样?像析构函数。我关心的是它是否是为所有类创建的,为什么需要默认构造函数?

回答by sbi

C++98/03

C++98/03

If they are needed,

如果他们需要,

  1. the compiler will generate a default constructorfor you unless you declare any constructor of your own.
  2. the compiler will generate a copyconstructorfor you unless you declare your own.
  3. the compiler will generate a copyassignment operatorfor you unless you declare your own.
  4. the compiler will generate a destructorfor you unless you declare your own.
  1. 除非您声明任何自己的构造函数,否则编译器将为您生成一个默认构造函数。
  2. 除非您声明自己的,否则编译器将为您生成一个复制构造函数
  3. 除非您声明自己的,否则编译器将为您生成一个复制赋值运算符
  4. 除非您声明自己的析构函数,否则编译器将为您生成一个析构函数

As Péter said in a helpful comment, all those are only generated by the compiler when they are needed. (The difference is that, when the compiler cannot create them, that's Ok as long as they aren't used.)

正如 Péter 在有用的评论中所说,所有这些仅在需要时由编译器生成。(不同之处在于,当编译器无法创建它们时,只要不使用它们就可以。)



C++11

C++11

C++11 adds the following rules, which are also true for C++14 (credits to towi, see this comment):

C++11 添加了以下规则,对于 C++14 也是如此(感谢 towi,请参阅此评论

  • The compiler generates the moveconstructorif
    • there is no user-declared copyconstructor, and
    • there is no user-declared copyassignment operator, and
    • there is no user-declared moveassignment operatorand
    • there is no user-declared destructor,
    • it is notmarked deleted,
    • and all members and bases are moveable.
  • Similarly for moveassignment operator, it is generated if
    • there is no user-declared copyconstructor, and
    • there is no user-declared copyassignment operator, and
    • there is no user-declared moveconstructorand
    • there is no user-declared destructor,
    • it is notmarked deleted,
    • and all members and bases are moveable.
  • 编译器生成移动构造函数,如果
    • 没有用户声明的复制构造函数,并且
    • 没有用户声明的复制赋值运算符,并且
    • 没有用户声明的移动赋值运算符
    • 没有用户声明的析构函数
    • 没有标记为deleted,
    • 并且所有成员和基地都是可移动的
  • 同样对于移动赋值运算符,如果
    • 没有用户声明的复制构造函数,并且
    • 没有用户声明的复制赋值运算符,并且
    • 没有用户声明的移动构造函数
    • 没有用户声明的析构函数
    • 没有标记为deleted,
    • 并且所有成员和基地都是可移动的

Note that these rules are a bit more elaborate than the C++03 rules and make more sense in practice.

请注意,这些规则比 C++03 规则要复杂一些,并且在实践中更有意义。

For an easier understanding of what is what in the above:

为了更容易地理解上面的内容:

class Thing {
public:
    Thing();                        // default constructor
    Thing(const Thing&);            // copy c'tor
    Thing& operator=(const Thing&); // copy-assign
    ~Thing();                       // d'tor
    // C++11:
    Thing(Thing&&);                 // move c'tor
    Thing& operator=(Thing&&);      // move-assign
};

Further reading: if you are a C++-beginner consider a design that does not require you to implement any of five a.k.a The Rule Of Zerooriginally from an articlewritten by Martinho Fernandes.

进一步阅读:如果您是 C++ 初学者,请考虑这样一种设计,它不需要您实现最初由Martinho Fernandes撰写的文章中的五个 aka零规则中的任何一个

回答by Chubsdad

Do you mean 'defined' by 'created'?

您的意思是“创建”是“定义”吗?

$12.1 - "The default constructor (12.1), copy constructor and copy assignment operator (12.8), and destructor (12.4) are special member functions.

$12.1 - “默认构造函数 (12.1)、复制构造函数和复制赋值运算符 (12.8) 和析构函数 (12.4) 是特殊的成员函数。

If 'created' means 'defined' then, here are the important parts from the C++ Standard.

如果“创建”意味着“定义”,那么这里是 C++ 标准中的重要部分。

-An implicitly-declared default constructor for a class is implicitly defined when it is used to create an object of its class type (1.8).

- 隐式声明的类的默认构造函数在用于创建其类类型 (1.8) 的对象时被隐式定义。

-If a class has no user-declared destructor, a destructor is declared implicitly. An implicitly-declared destructor is implicitly defined when it is used to destroy an object of its class type.

- 如果一个类没有用户声明的析构函数,则隐式声明一个析构函数。隐式声明的析构函数在用于销毁其类类型的对象时是隐式定义的。

-If the class definition does not explicitly declare a copy constructor, one is declared implicitly. An implicitly-declared copy constructor is implicitly defined if it is used to initialize an object of its class type from a copy of an object of its class type or of a class type derived from its class type).

- 如果类定义没有显式声明复制构造函数,则隐式声明一个。如果隐式声明的复制构造函数用于从其类类型的对象的副本或从其类类型派生的类类型的副本中初始化其类类型的对象,则它是隐式定义的)。

-If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly. An implicitly-declared copy assignment operator is implicitly defined when an object of its class type is assigned a value of its class type or a value of a class type derived from its class type.

- 如果类定义未显式声明复制赋值运算符,则隐式声明一个。当为其类类型的对象分配其类类型的值或从其类类型派生的类类型的值时,隐式声明的复制赋值运算符是隐式定义的。

回答by Klaim

By default, if not implemented by the user, the compiler add some member functions to the class. Those are called the big four :

默认情况下,如果用户没有实现,编译器会在类中添加一些成员函数。那些被称为四大:

  • default constructor
  • copy constructor
  • copy operator (assignment)
  • destructor
  • 默认构造函数
  • 复制构造函数
  • 复制运算符(赋值)
  • 析构函数

Depending on the types of the members and which member function listed you provide yourself, those will not all be generated.

根据成员的类型和您自己提供的成员函数,这些不会全部生成。

回答by Tony Delroy

Other answers have told you what's created, and that the compiler may only generate them if used.

其他答案已经告诉您创建了什么,并且编译器可能只在使用时生成它们。

My concern is whether it is created for all the classes...

我担心的是它是否是为所有类创建的...

Why concerned? Thinking it's creating unwanted code in the executable? Unlikely, but you can check easily enough with your environment.

为什么关心?认为它在可执行文件中创建了不需要的代码?不太可能,但您可以很容易地检查您的环境。

Or perhaps your concern was that it might not create a constructor when you want one? Nothing to worry about... they're always created if needed and not provided by the user.

或者您可能担心当您需要构造函数时它可能不会创建构造函数?没什么可担心的……它们总是在需要时创建,而不是由用户提供。

...and why is default constructor needed?

...为什么需要默认构造函数?

Because classes may have objects inside them with their own destructors that need to be systematically invoked. For example, given...

因为类中可能有对象,它们有自己的析构函数,需要系统地调用。例如,给定...

struct X
{
    std::string a;
    std::string b;
};

...the default destructor makes sure the destructors for a and b run.

...默认析构函数确保 a 和 b 的析构函数运行。