The order problem of C + + automatic construction

Posted on

Automatic deconstruction is constructed after deconstruction

//Member templates for normal (non template) classes
class DebugDelete{
public:
 DebugDelete(ostream &s = cerr) :os(s){}
 template <typename T>void operator()(T*p)const
 {
 os << "deleting unique_ptr " <<typeid(T).name() <<endl;
 delete p;
 }
private:
 ostream &os;
};
void demo_general_class_tempalte_member()
{
 double *p = new double;
 DebugDelete d;
 d(p);// D call debugdelet:: operator() (double *) to release P
 int* ip = new int;
 //Call operator () (int *) on a temporary debugdelete object
 DebugDelete()(ip);
 //Instantiate debugdelete:: operator() < int > (int *) const
 unique_ptr<int, DebugDelete>p2(new int, DebugDelete());
 //Instantiate debugdelete:: operator() < string > (string *) const
 unique_ptr<string, DebugDelete>sp(new string, DebugDelete());
}

Output here

deleting unique_ptr  double
deleting unique_ptr  int
deleting unique_ptr  class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
deleting unique_ptr  int

It can be seen that the active deconstruction proceeds normally

Function after the end of the automatic deconstruction, first created the int and then created a string, but first deconstructed the string


class B
{
public:
 virtual ~B(){ cout << "delete B" << endl; }
};
class D :B
{
public:
 virtual ~D() override{ cout << "delete D" << endl; }
};
void demo_delete()
{
 D d;
}

output

delete D

delete B

In this case, the base class is constructed first and then the derived class is constructed. However, the subclass is constructed first and then the base class is constructed.

Knowledge point supplement: execution sequence of C + + construction and Deconstruction

1. The code is as follows:


class A
{
public:
 int _Id; 
 A():_Id(0)
 {
 printf("A[%d]n",_Id);
 }

~A()
 {
 printf("~A[%d]n",_Id);
 }
};

class B
{
public:
 A _A;
 A* _PA;
 B()
 {
 printf("Bn");
 }
 ~B()
 {
 printf("~Bn");
 delete _PA;
 }
};

int main(int argc, char* argv[])
{
 {
 B b;
 b._PA = new A();
 b._PA->_Id = 17;
 }

 return 0;
}

2. Execution sequence

A[0]
B
A[0]
~B
~A[17]
~A[0]

3. B is the object on the stack. C + + guarantees that the object on the stack will leave the scope and call the destructor method automatically.

4. Consider the objects in B_ A is the object on the stack_ PA is a pointer, an object on the heap_ PA must be deleted, otherwise the resource is leaked. And for_ A does not need to be processed, and the destruct method is called automatically. It can be understood that object B leaves the scope and calls the destructor method, while the_ Of course, a also leaves the scope (where there is no skin, there will be no hair) and calls the deconstruction method.

5. Encountered such a situation, vs automatic generation of deconstruction method has a problem, leading to crash. Just add a destructor manually.

summary

So far, this article about the order of C + + automatic deconstruction is introduced here. For more information about the order of C + + automatic deconstruction, please search previous articles of developer or continue to browse the following related articles. I hope you can support developer more in the future!

Leave a Reply

Your email address will not be published.