<small id='4Ooka'></small><noframes id='4Ooka'>

  1. <i id='4Ooka'><tr id='4Ooka'><dt id='4Ooka'><q id='4Ooka'><span id='4Ooka'><b id='4Ooka'><form id='4Ooka'><ins id='4Ooka'></ins><ul id='4Ooka'></ul><sub id='4Ooka'></sub></form><legend id='4Ooka'></legend><bdo id='4Ooka'><pre id='4Ooka'><center id='4Ooka'></center></pre></bdo></b><th id='4Ooka'></th></span></q></dt></tr></i><div id='4Ooka'><tfoot id='4Ooka'></tfoot><dl id='4Ooka'><fieldset id='4Ooka'></fieldset></dl></div>
  2. <legend id='4Ooka'><style id='4Ooka'><dir id='4Ooka'><q id='4Ooka'></q></dir></style></legend>
    1. <tfoot id='4Ooka'></tfoot>
      • <bdo id='4Ooka'></bdo><ul id='4Ooka'></ul>

      什么是 C++ 中的就地构造函数?

      What is an in-place constructor in C++?(什么是 C++ 中的就地构造函数?)

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

            • <bdo id='0pWFR'></bdo><ul id='0pWFR'></ul>
              <legend id='0pWFR'><style id='0pWFR'><dir id='0pWFR'><q id='0pWFR'></q></dir></style></legend>
            • <tfoot id='0pWFR'></tfoot>

            • <small id='0pWFR'></small><noframes id='0pWFR'>

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

                问题描述

                可能的重复:
                C++的“placement new”

                什么是 C++ 中的就地构造函数?

                What is an in-place constructor in C++?

                例如Datatype *x = new(y) Datatype();

                推荐答案

                这称为放置新操作符.它允许您提供将分配数据的内存,而无需 new 运算符分配它.例如:

                This is called the placement new operator. It allows you to supply the memory the data will be allocated in without having the new operator allocate it. For example:

                Foo * f = new Foo();
                

                上面会为你分配内存.

                void * fm = malloc(sizeof(Foo));
                Foo *f = new (fm) Foo(); 
                

                以上将使用调用malloc分配的内存.new 不会再分配了.但是,您不仅限于课程.您可以对通过调用 new 分配的任何类型使用放置 new 运算符.

                The above will use the memory allocated by the call to malloc. new will not allocate any more. You are not, however, limited to classes. You can use a placement new operator for any type you would allocate with a call to new.

                placement new 的一个问题"是,您不应该释放通过使用delete 关键字调用placement new 运算符所分配的内存.您将通过直接调用析构函数来销毁对象.

                A 'gotcha' for placement new is that you should not release the memory allocated by a call to the placement new operator using the delete keyword. You will destroy the object by calling the destructor directly.

                f->~Foo();
                

                手动调用析构函数后,内存可以按预期释放.

                After the destructor is manually called, the memory can then be freed as expected.

                free(fm);
                

                这篇关于什么是 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(使用非平凡的构造函数初始化联合)
                    <bdo id='J2cWO'></bdo><ul id='J2cWO'></ul>
                    <i id='J2cWO'><tr id='J2cWO'><dt id='J2cWO'><q id='J2cWO'><span id='J2cWO'><b id='J2cWO'><form id='J2cWO'><ins id='J2cWO'></ins><ul id='J2cWO'></ul><sub id='J2cWO'></sub></form><legend id='J2cWO'></legend><bdo id='J2cWO'><pre id='J2cWO'><center id='J2cWO'></center></pre></bdo></b><th id='J2cWO'></th></span></q></dt></tr></i><div id='J2cWO'><tfoot id='J2cWO'></tfoot><dl id='J2cWO'><fieldset id='J2cWO'></fieldset></dl></div>
                        <tbody id='J2cWO'></tbody>
                    • <tfoot id='J2cWO'></tfoot>

                      1. <legend id='J2cWO'><style id='J2cWO'><dir id='J2cWO'><q id='J2cWO'></q></dir></style></legend>

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