<legend id='Vvokx'><style id='Vvokx'><dir id='Vvokx'><q id='Vvokx'></q></dir></style></legend>

    <tfoot id='Vvokx'></tfoot>
  1. <i id='Vvokx'><tr id='Vvokx'><dt id='Vvokx'><q id='Vvokx'><span id='Vvokx'><b id='Vvokx'><form id='Vvokx'><ins id='Vvokx'></ins><ul id='Vvokx'></ul><sub id='Vvokx'></sub></form><legend id='Vvokx'></legend><bdo id='Vvokx'><pre id='Vvokx'><center id='Vvokx'></center></pre></bdo></b><th id='Vvokx'></th></span></q></dt></tr></i><div id='Vvokx'><tfoot id='Vvokx'></tfoot><dl id='Vvokx'><fieldset id='Vvokx'></fieldset></dl></div>

    <small id='Vvokx'></small><noframes id='Vvokx'>

        <bdo id='Vvokx'></bdo><ul id='Vvokx'></ul>
    1. 什么是 C++ 中的非平凡构造函数?

      What is a non-trivial constructor in C++?(什么是 C++ 中的非平凡构造函数?)
      <tfoot id='KYIlQ'></tfoot>
        <tbody id='KYIlQ'></tbody>
      1. <legend id='KYIlQ'><style id='KYIlQ'><dir id='KYIlQ'><q id='KYIlQ'></q></dir></style></legend>
          <bdo id='KYIlQ'></bdo><ul id='KYIlQ'></ul>
            • <small id='KYIlQ'></small><noframes id='KYIlQ'>

                <i id='KYIlQ'><tr id='KYIlQ'><dt id='KYIlQ'><q id='KYIlQ'><span id='KYIlQ'><b id='KYIlQ'><form id='KYIlQ'><ins id='KYIlQ'></ins><ul id='KYIlQ'></ul><sub id='KYIlQ'></sub></form><legend id='KYIlQ'></legend><bdo id='KYIlQ'><pre id='KYIlQ'><center id='KYIlQ'></center></pre></bdo></b><th id='KYIlQ'></th></span></q></dt></tr></i><div id='KYIlQ'><tfoot id='KYIlQ'></tfoot><dl id='KYIlQ'><fieldset id='KYIlQ'></fieldset></dl></div>

                本文介绍了什么是 C++ 中的非平凡构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在阅读这个http://en.wikipedia.org/wiki/C%2B%2B0x#Modification_to_the_definition_of_plain_old_data

                它提到了平凡的默认构造函数、平凡的复制构造函数、复制赋值运算符、平凡的析构函数.什么是平凡和不平凡?

                It mentions trivial default constructor, trivial copy constructor, copy assignment operator, trivial destructor. What is trivial and not trivial?

                推荐答案

                简单来说,平凡"的特殊成员函数的字面意思是一个以非常直接的方式完成其工作的成员函数.直截了当"对于不同类型的特殊成员函数意味着不同的东西.

                In simple words a "trivial" special member function literally means a member function that does its job in a very straightforward manner. The "straightforward manner" means different thing for different kinds of special member functions.

                对于默认的构造函数和析构函数,微不足道"的字面意思是什么都不做".对于复制构造函数和复制赋值运算符,平凡"的字面意思是等同于简单的原始内存复制"(例如使用 memcpy 进行复制).

                For a default constructor and destructor being "trivial" means literally "do nothing at all". For copy-constructor and copy-assignment operator, being "trivial" means literally "be equivalent to simple raw memory copying" (like copy with memcpy).

                如果你自己定义了一个构造函数,它被认为是非平凡的,即使它什么都不做,所以一个平凡的构造函数必须由编译器隐式定义.

                If you define a constructor yourself, it is considered non-trivial, even if it doesn't do anything, so a trivial constructor must be implicitly defined by the compiler.

                为了使特殊的成员函数满足上述要求,类必须具有非常简单的结构,在创建或销毁对象时不得要求任何隐藏的初始化,或在创建或销毁对象时进行任何隐藏的附加内部操作被复制.

                In order for a special member function to satisfy the above requirements, the class must have a very simplistic structure, it must not require any hidden initializations when an object is being created or destroyed, or any hidden additional internal manipulations when it is being copied.

                例如,如果类有虚函数,那么在创建这个类的对象时需要一些额外的隐藏初始化(初始化虚方法表等),所以这个类的构造函数不会是微不足道的.

                For example, if class has virtual functions, it will require some extra hidden initializations when objects of this class are being created (initialize virtual method table and such), so the constructor for this class will not qualify as trivial.

                再举一个例子,如果一个类有虚拟基类,那么这个类的每个对象都可能包含指向同一对象其他部分的隐藏指针.这样的自引用对象无法通过简单的原始内存复制例程(如 memcpy)复制.需要额外的操作来正确地重新初始化副本中的隐藏指针.出于这个原因,此类的复制构造函数和复制赋值运算符将不符合微不足道的条件.

                For another example, if a class has virtual base classes, then each object of this class might contain hidden pointers that point to other parts of the very same object. Such a self-referential object cannot be copied by a simple raw memory copy routine (like memcpy). Extra manipulations will be necessary to properly re-initialize the hidden pointers in the copy. For this reason the copy constructor and copy-assignment operator for this class will not qualify as trivial.

                出于显而易见的原因,此要求是递归的:类的所有子对象(基类和非静态成员)还必须具有普通构造函数.

                For obvious reasons, this requirement is recursive: all subobjects of the class (bases and non-static members) must also have trivial constructors.

                这篇关于什么是 C++ 中的非平凡构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

                    <tbody id='AhNyU'></tbody>
                    <bdo id='AhNyU'></bdo><ul id='AhNyU'></ul>
                    • <tfoot id='AhNyU'></tfoot>

                        <legend id='AhNyU'><style id='AhNyU'><dir id='AhNyU'><q id='AhNyU'></q></dir></style></legend>

                      • <small id='AhNyU'></small><noframes id='AhNyU'>

                        <i id='AhNyU'><tr id='AhNyU'><dt id='AhNyU'><q id='AhNyU'><span id='AhNyU'><b id='AhNyU'><form id='AhNyU'><ins id='AhNyU'></ins><ul id='AhNyU'></ul><sub id='AhNyU'></sub></form><legend id='AhNyU'></legend><bdo id='AhNyU'><pre id='AhNyU'><center id='AhNyU'></center></pre></bdo></b><th id='AhNyU'></th></span></q></dt></tr></i><div id='AhNyU'><tfoot id='AhNyU'></tfoot><dl id='AhNyU'><fieldset id='AhNyU'></fieldset></dl></div>