• <bdo id='FkFvw'></bdo><ul id='FkFvw'></ul>

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

    <tfoot id='FkFvw'></tfoot>
    <legend id='FkFvw'><style id='FkFvw'><dir id='FkFvw'><q id='FkFvw'></q></dir></style></legend>

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

        公共运营商新建,私有运营商删除:获取C2248“无法访问私有成员"当使用新

        Public operator new, private operator delete: getting C2248 quot;can not access private memberquot; when using new(公共运营商新建,私有运营商删除:获取C2248“无法访问私有成员当使用新)

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

          <tbody id='KNs5d'></tbody>

        1. <tfoot id='KNs5d'></tfoot>
                <bdo id='KNs5d'></bdo><ul id='KNs5d'></ul>
              • <legend id='KNs5d'><style id='KNs5d'><dir id='KNs5d'><q id='KNs5d'></q></dir></style></legend>

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

                  本文介绍了公共运营商新建,私有运营商删除:获取C2248“无法访问私有成员"当使用新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  一个类具有重载运算符 newdelete.new 是公开的,delete 是私有的.

                  A class has overloaded operators new and delete. new is public, delete is private.

                  在构造此类的实例时,出现以下错误:

                  When constructing an instance of this class, I get the following error:

                  pFoo = new Foo(bar)
                  

                  example.cpp(1): 错误 C2248: 'Foo:operator delete': 无法访问类 'Foo' 中声明的私有成员

                  但是这里没有调用delete,那么编译器的扭曲思想是怎么回事?:)

                  But there's no call to delete here, so what is going on in the twisted mind of the compiler? :)

                  1. 错误的原因是什么?
                  2. 是否可以在不求助于成员 CreateInstance 函数的情况下解决问题?
                  1. What is the reason for the error?
                  2. Is it possible to resolve the problem without resorting to a member CreateInstance function?

                  推荐答案

                  当你执行 new Foo() 时,会发生两件事:首先调用 operator new 来分配内存,然后调用 Foo 的构造函数.如果该构造函数抛出异常,因为您无法访问已分配的内存,C++ 运行时将通过将其传递给适当的 operator delete 来处理它.这就是为什么你总是必须为你编写的每个 operator new 实现一个匹配的 operator delete 并且这就是它需要可访问的原因.

                  When you do new Foo() then two things happen: First operator new is invoked to allocate memory, then a constructor for Foo is called. If that constructor throws, since you cannot access the memory already allocated, the C++ runtime will take care of it by passing it to the appropriate operator delete. That's why you always must implement a matching operator delete for every operator new you write and that's why it needs to be accessible.

                  作为一种出路,您可以将它们都设为私有并从公共成员函数(如 create())调用 operator new.

                  As a way out you could make both of them private and invoke operator new from a public member function (like create()).

                  这篇关于公共运营商新建,私有运营商删除:获取C2248“无法访问私有成员"当使用新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                      <tbody id='H0qgI'></tbody>
                      <bdo id='H0qgI'></bdo><ul id='H0qgI'></ul>
                      <i id='H0qgI'><tr id='H0qgI'><dt id='H0qgI'><q id='H0qgI'><span id='H0qgI'><b id='H0qgI'><form id='H0qgI'><ins id='H0qgI'></ins><ul id='H0qgI'></ul><sub id='H0qgI'></sub></form><legend id='H0qgI'></legend><bdo id='H0qgI'><pre id='H0qgI'><center id='H0qgI'></center></pre></bdo></b><th id='H0qgI'></th></span></q></dt></tr></i><div id='H0qgI'><tfoot id='H0qgI'></tfoot><dl id='H0qgI'><fieldset id='H0qgI'></fieldset></dl></div>

                        <legend id='H0qgI'><style id='H0qgI'><dir id='H0qgI'><q id='H0qgI'></q></dir></style></legend><tfoot id='H0qgI'></tfoot>

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