简体   繁体   中英

Constructors and destructors

I have the following code and as I knew,at the end of a program which uses class constructors,if certain objects were created,they're destroyed.Judging by that,at the end of the execution I should've had some "~B()" and "~D()" printed out in a particular order but that's not happening when I'm runing the code.Why's that?

#include<iostream>
#include<stdlib.h>
using namespace std;

class B{
public:
    B(){cout<<"B()";}
    virtual void print(){cout<<"b";}
    ~B(){cout<<"~B()";}
};

class D:public B{
public:
    D(){cout<<"D()";}
    void print(){B::print()
    ;cout<<"d";}
    ~D(){cout<<"~D()";}
    };

void testI(){
    B* b[]={new B(),new D()};
    b[1]->print();
    B&c=*b[1];
    c.print();
}



int main(){
    testI();
return 0;
}

You are creating your objects with new , this means that they are allocated on the heap and NOT the stack and there fore it is up to you to delete them.

B * b = new B();

Later on..

delete b;

Edit:

For arrays use:

delete[] b; //if b is a pointer to an array of B's

Because you use dynamic allocation. And for that you are responsible of destroying what you allocate.

read about new and delete here: http://www.cplusplus.com/reference/new/

You're using new to allocate dynamic memory without deleting objects. While you can solve this by adding a delete statement,as your code gets more complex you'll find manual memory management can get unwieldy and error-prone.

You'll be much better off using the automatic memory management classes like std::unique_ptr and std::shared_ptr , and using container classes like std::vector .

void testI()
{
    std::vector<std::shared_ptr<B>> b = {std::make_shared<B>(), std::make_shared<D>()};
    b[1]->print();
    B& c= *b[1];
    c.print();
} //b is destroyed here

In the norm:

3.6.1.5 - A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration)

So, during your program, you create several variables. When returning from main, only those with automatic storage duration are destroyed. Those with dynamic storage duration are not. For your variables with dynamic storage duration, you have to explicitly call delete .

Here is a small example. Put a breakpoint in the destructor and check for m_name value.

#include <iostream>
#include <string>

class A
{
public:
    A(const std::string &name):m_name(name){}

    ~A()
    {
        std::cout<<"deleting "<<m_name<<std::endl;
    }

private:
    std::string m_name;
};

A a("Variable at namespace scope");

int main()
{
    A a0("Automatic storage"); 
    A *a1 = new A("Dynamic storage 1"); 
    A *a2 = new A("Dynamic storage 2"); 
    delete a2;
    static A a3("Static storage");

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM