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

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

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

        <bdo id='HBJ6C'></bdo><ul id='HBJ6C'></ul>
      <tfoot id='HBJ6C'></tfoot>
    1. 删除指向子类的指针是否调用基类析构函数?

      时间:2023-07-19
        <bdo id='Lln1B'></bdo><ul id='Lln1B'></ul>
        • <legend id='Lln1B'><style id='Lln1B'><dir id='Lln1B'><q id='Lln1B'></q></dir></style></legend>
          <i id='Lln1B'><tr id='Lln1B'><dt id='Lln1B'><q id='Lln1B'><span id='Lln1B'><b id='Lln1B'><form id='Lln1B'><ins id='Lln1B'></ins><ul id='Lln1B'></ul><sub id='Lln1B'></sub></form><legend id='Lln1B'></legend><bdo id='Lln1B'><pre id='Lln1B'><center id='Lln1B'></center></pre></bdo></b><th id='Lln1B'></th></span></q></dt></tr></i><div id='Lln1B'><tfoot id='Lln1B'></tfoot><dl id='Lln1B'><fieldset id='Lln1B'></fieldset></dl></div>

            <tfoot id='Lln1B'></tfoot>
              <tbody id='Lln1B'></tbody>

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

                本文介绍了删除指向子类的指针是否调用基类析构函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个 class A,它为它的一个字段使用堆内存分配.类 A 被实例化并作为指针字段存储在另一个类中(class B.

                I have an class A which uses a heap memory allocation for one of its fields. Class A is instantiated and stored as a pointer field in another class (class B.

                当我处理完 B 类的对象时,我调用 delete,我假设它调用了析构函数...但这是否也调用了类 A 的析构函数?

                When I'm done with an object of class B, I call delete, which I assume calls the destructor... But does this call the destructor of class A as well?

                从答案中,我认为(如果不正确,请编辑):

                From the answers, I take that (please edit if incorrect):

                1. delete B 的实例调用 B::~B();
                2. 调用A::~A();
                3. A::~A 应该显式delete A对象的所有堆分配的成员变量;
                4. 最后,存储所述类 B 实例的内存块返回到堆 - 当 new 被使用时,它首先在堆上分配一块内存,然后调用构造函数来初始化它,现在之后已调用所有析构函数来终结对象,对象所在的块将返回到堆中.
                1. delete of an instance of B calls B::~B();
                2. which calls A::~A();
                3. A::~A should explicitly delete all heap-allocated member variables of the A object;
                4. Finally the memory block storing said instance of class B is returned to the heap - when new was used, it first allocated a block of memory on heap, then invoked constructors to initialize it, now after all destructors have been invoked to finalize the object the block where the object resided is returned to the heap.

                推荐答案

                A 的析构函数将在其生命周期结束时运行.如果您希望释放其内存并运行析构函数,则必须将其删除(如果它是在堆上分配的).如果它是在堆栈上分配的,这会自动发生(即当它超出范围时;请参阅 RAII).如果它是类的成员(不是指针,而是完整成员),那么当包含对象被销毁时就会发生这种情况.

                The destructor of A will run when its lifetime is over. If you want its memory to be freed and the destructor run, you have to delete it if it was allocated on the heap. If it was allocated on the stack this happens automatically (i.e. when it goes out of scope; see RAII). If it is a member of a class (not a pointer, but a full member), then this will happen when the containing object is destroyed.

                class A
                {
                    char *someHeapMemory;
                public:
                    A() : someHeapMemory(new char[1000]) {}
                    ~A() { delete[] someHeapMemory; }
                };
                
                class B
                {
                    A* APtr;
                public:
                    B() : APtr(new A()) {}
                    ~B() { delete APtr; }
                };
                
                class C
                {
                    A Amember;
                public:
                    C() : Amember() {}
                    ~C() {} // A is freed / destructed automatically.
                };
                
                int main()
                {
                    B* BPtr = new B();
                    delete BPtr; // Calls ~B() which calls ~A() 
                    C *CPtr = new C();
                    delete CPtr;
                    B b;
                    C c;
                } // b and c are freed/destructed automatically
                

                在上面的例子中,每次删除和删除[]都是需要的.并且不需要删除(或确实可以使用)我没有使用它的地方.

                In the above example, every delete and delete[] is needed. And no delete is needed (or indeed able to be used) where I did not use it.

                auto_ptrunique_ptrshared_ptr 等...非常适合使生命周期管理更容易:

                auto_ptr, unique_ptr and shared_ptr etc... are great for making this lifetime management much easier:

                class A
                {
                    shared_array<char> someHeapMemory;
                public:
                    A() : someHeapMemory(new char[1000]) {}
                    ~A() { } // someHeapMemory is delete[]d automatically
                };
                
                class B
                {
                    shared_ptr<A> APtr;
                public:
                    B() : APtr(new A()) {}
                    ~B() {  } // APtr is deleted automatically
                };
                
                int main()
                {
                    shared_ptr<B> BPtr = new B();
                } // BPtr is deleted automatically
                

                这篇关于删除指向子类的指针是否调用基类析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何获取使用 malloc() 分配的内存块的大小? 下一篇:我可以将 std::string 传递给 DLL 吗?

                相关文章

                  <small id='5WOC8'></small><noframes id='5WOC8'>

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