编译器为类创建的所有成员函数是什么?这是否一直发生? [英] What are all the member-functions created by compiler for a class? Does that happen all the time?

查看:237
本文介绍了编译器为类创建的所有成员函数是什么?这是否一直发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

+98/03

如果需要,


  1. 默认构造函数,除非您声明自己的任何构造函数。

  2. 除非您声明自己的版本,否则编译器将为您生成 复制 构造函数。

  3. 除非您声明自己的版本,否则编译器将为您生成 复制 赋值运算符。

  4. 编译器将为您生成析构函数,除非您声明自己的。

正如Péter在一篇有用的评论中所说的,所有这些都只有在需要时由编译器生成 。 (不同的是,当编译器不能创建它们时,只要没有使用就没关系。)






C ++ 11



C ++ 11添加了以下规则,这也适用于C ++ 14 =http://stackoverflow.com/questions/3734247/what-are-all-the-member-functions-created-by-compiler-for-a-class-does-that-hap/3734268?noredirect=1# comment30075900_3734268>此评论)




  • 编译器生成如果


    • 没有用户声明的 复制 构造函数

    • 没有用户声明的 复制 分配运算符和 / li>
    • 没有用户声明的析构

    • / strong>标记为已删除

    • ,所有成员和基数均为可移动


      • 移动 类似:如果没有用户定义


        • 没有用户声明的复制 构造函数 / li>
        • 没有用户声明的 复制 分配运算符

        • 没有用户声明的 移动 构造函数

        • 析构

        • 标记为已删除 li>
        • 且所有成员和基数都可移动




      请注意,这些规则比C ++ 03规则更加详细,在实践中更有意义。



      理解什么是上面这里的 Thing 的项目:

        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
      };

      作为进一步的阅读,如果你是一个C ++ - 初学者考虑一个不需要你的设计实施最后五个,也就是 零规则 (通过 Martinho Fernandes


      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?

      解决方案

      C++98/03

      If they are needed,

      1. the compiler will generate a default constructor for you unless you declare any constructor of your own.
      2. the compiler will generate a copy constructor for you unless you declare your own.
      3. the compiler will generate a copy assignment operator for you unless you declare your own.
      4. the compiler will generate a destructor for you unless you declare your own.

      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.)


      C++11

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

      • The compiler generates the move constructor if
        • there is no user-declared copy constructor, and
        • there is no user-declared copy assignment operator, and
        • there is no user-declared move assignment operator and
        • there is no user-declared destructor,
        • it is not marked as deleted,
        • and all members and bases are moveable.
      • Similar for the move assignment operator: It is generated if there is no user defined
        • there is no user-declared copy constructor, and
        • there is no user-declared copy assignment operator, and
        • there is no user-declared move constructor and
        • there is no user-declared destructor,
        • it is not marked as deleted,
        • and all members and bases are moveable.

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

      For an easier understanding of what is what of the above here the items for Thing:

      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
      };
      

      And as further reading, if you are a C++-beginner consider a design that does not require you to implement any of the last five, a.k.a The Rule Of Zero (by Martinho Fernandes).

      这篇关于编译器为类创建的所有成员函数是什么?这是否一直发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆