The C++ class member constructor and destructor sequence examples are explained in detail

  • 2020-04-02 01:59:16
  • OfStack

Objects are not created all of a sudden. To create an object, you must create both the parent class and the object contained within it. C++ follows the following creation order:

(1) if a class is a specific base class, execute the default constructor of the base class.

(2) non-static data members of the class, created in declared order.

(3) execute the constructor of the class.

That is, when a class is constructed, its superclass is constructed, its class members are created, and its constructor is called.

Here's an example


class c
{
public:
    c(){ printf("cn"); }
protected:
private:
};
class b 
{
public:
    b(){ printf("bn");}
protected:
    c C;
private:
};
class a : public b
{
public:
    a(){ printf("an"); }
protected:
private:
};
int main()
{
    a A;
    getchar();
}


< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / img.jbzj.com/file_images/article/201311/20131126100347.jpg? 2013102610540 ">

Analyze it   First of all, we define three classes a, b, c, where a inherits from b, and we construct ain the main function, because a inherits from b, we construct b first, and then b has a member c, so c is constructed first, then b, and then a.

Here's an example:


class c
{
public:
    c(){ printf("cn"); }
protected:
private:
};
class b 
{
public:
    b(){ printf("bn");}
protected:
private:
};
class a : public b
{
public:
    a(){ printf("an"); }
protected:
    c C;
private:
};
int main()
{
    a A;
    getchar();
}

It didn't change much, it just added a member of c to a and removed b.

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / img.jbzj.com/file_images/article/201311/20131126100428.jpg? 2013102610640 ">

A is also constructed in main, a inherited from b, so first construct b, and then construct the data member c of a itself, and finally call the constructor of a itself.

Here you get the details of the construction.

Now let's look at the order of destruction:

(1) call the destructor of the class.

(2) destroy the data members in the opposite order of creation.

(3) if there is a parent class, call the destructor of the parent class.

Here's another example:


class c
{
public:
    c(){}
    ~c(){ printf("cn"); }
protected:
private:
};
class b 
{
public:
    b(){}
    ~b(){ printf("bn");}
protected:
private:
};
class a : public b
{
public:
    a(){}
    ~a(){ printf("an"); }
protected:
    c C;
private:
};
int main()
{
    a A;
    return 0;
}


< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / img.jbzj.com/file_images/article/201311/20131126100506.jpg? 2013102610715 ">

The procedure is that when the main function ends, a will be destroyed, so the destructor of a will be called first, and then the data member c of a will be destroyed, and finally the parent class b of a will be destroyed. It's the reverse of the order in which it was created.

All right, so far I think you've figured out the mystery of tectonic destructions.


Related articles: