当前位置:网站首页>Constructor, class member, destructor call order
Constructor, class member, destructor call order
2022-06-30 11:53:00 【Adunn】
1. Constructors 、 Members of the class 、 Destructor call order
C++ The calling order of the constructor is :
Members of the base class -> The constructor body of the base class -> Members of derived classes -> Constructor body of derived class
C++ The order in which destructors are called is :
Destructors of derived classes -> Member destructors of derived classes -> The destructor of the base class -> Member destructor of base class
- Example 1: Constructors 、 Class member call order
- Example 2: Base class 、 Derived class constructors ( Destructor ) Call to order
- Example 3: comprehensive : Base class 、 Derived class constructors ( Destructor )、 Class member call order
- Example 4: comprehensive : Base class 、 Derived class virtual destructor (virtual Virtual functions ) Call to order
1.1. Example 1: Constructors 、 Class member call order
#include <iostream>
class ParentMember
{
public:
ParentMember()
{
std::cout<<"ParentMember Construct"<<std::endl;
}
~ParentMember()
{
std::cout<<"ParentMember Destruct"<<std::endl;
}
};
class Parent
{
public:
Parent()
{
std::cout<<"Parent Construct"<<std::endl;
}
~Parent()
{
std::cout<<"Parent Destruct"<<std::endl;
}
private:
ParentMember parentMember;
};
int main()
{
cout<<"Construct The process : "<<endl;
Parent *p = new Parent;
cout<<""<<endl;
cout<<"Destruct The process : "<<endl;
delete p;
}
Running results :
Construct The process :
ParentMember Construct
Parent Construct
Destruct The process :
Parent Destruct
ParentMember Destruct
It's not hard to see from the running results :
1、 In the process of construction , The order of execution is : Members of the class -> Class constructor body .
If breakpoint debugging , Will find , First run to the constructor '{' It's about , You will turn around and call the member variable first .
2、 In the course of deconstruction , The order of execution is : Destructor of class -> Class member destructs
1.2. Example 2: Base class 、 Derived class constructors ( Destructor ) Call to order
Base class 、 The order in which derived classes are constructed
- The base (base) -> The derived (derived); That is, first construct the base class , Construct derived classes again ;
- because Base class Is independent of derived classes , That is, objects in derived classes will not be called , So it should be generated first ;
- If the derived class is generated before the base class , Because the base class resource cannot be called , Generation may fail ;
Base class 、 The order in which derived classes are destructed
- The derived (derived) -> The base (base); That is, release the derived class first , Release the base class ;
- because Derived class You need to release the called base class resources first , Therefore, priority should be given to release ;
- If the base class is destructed first , Some resources may be occupied by derived classes , May cause destruct failure ;
#include <iostream>
class Parent
{
public:
Parent()
{
std::cout<<"Parent Construct"<< std::endl;
}
~Parent()
{
std::cout<<"Parent Destruct"<< std::endl;
}
};
class Child: public Parent
{
public:
Child()
{
std::cout<<"Child Construct"<< std::endl;
}
~Child()
{
std::cout<<"Child Destruct"<< std::endl;
}
};
int main()
{
std::cout<<"Construct The process : "<< std::endl;
Parent *p = new Child;
std::cout<<" "<< std::endl;
std::cout<<"Destruct The process : "<< std::endl;
delete p;
}
Running results :
Construct The process :
Parent Construct
Child Construct
Destruct The process :
Parent Destruct
It's not hard to see from the running results :
1、 In the process of construction , The order of execution is : First base class , The latter subclass , namely : The constructor body of the base class -> The constructor body of a subclass
If breakpoint debugging , Will find , First run to the subclass constructor '{' It's about , You will call the base class member variable first .
2、 In the course of deconstruction , The order of execution is : How can we just base classes ?
The order of execution should have been : Antecedent subclass , Post base class , That is, the destructor of the subclass -> The destructor of the base class . But it is not difficult for us to find out the actual operation effect , Release the pointer at the end p The object pointed to , Only part of the base class is released , No part of the derived class is released . This is because , The pointer p When declared, it is a pointer to the base class , The destructor of the base class is not an imaginary function , So calling that destructor is determined at compile time . If you want to perform the correct destruct order , The destructor of the base class needs to be defined as virtual, In this way, the destructor of the derived class is automatically virtual Of course. , Release the pointer at the end p when , according to RTTI, perform p The destructor of the real object .
class Parent
{
public:
Parent()
{
std::cout<<"Parent Construct"<< std::endl;
}
virtual ~Parent() // increase virtual
{
std::cout<<"Parent Destruct"<< std::endl;
}
};
Running results after modification :
Construct The process :
Parent Construct
Child Construct
Destruct The process :
Child Destruct
Parent Destruct
1.3. Example 3: comprehensive : Base class 、 Derived class constructors ( Destructor )、 Class member call order
#include <iostream>
class ParentMember
{
public:
ParentMember()
{
std::cout<<"ParentMember Construct"<< std::endl;
}
~ParentMember()
{
std::cout<<"ParentMember Destruct"<< std::endl;
}
};
class Parent
{
public:
Parent()
{
std::cout<<"Parent Construct"<< std::endl;
}
~Parent()
{
std::cout<<"Parent Destruct"<< std::endl;
}
private:
ParentMember parentMember;
};
class ChildMember
{
public:
ChildMember()
{
std::cout<<"ChildMember Construct"<< std::endl;
}
~ChildMember()
{
// std::cout<<"ChildMember Destruct"<< std::endl;
}
};
class Child: public Parent
{
public:
Child()
{
std::cout<<"Child Construct"<< std::endl;
}
~Child()
{
std::cout<<"Child Destruct"<< std::endl;
}
private:
ChildMember childMember;
};
int main()
{
std::cout<<"Construct The process : "<< std::endl;
and Parent *p = new Child;
std::cout<<""<< std::endl;
std::cout<<"Destruct The process : "<< std::endl;
delete p;
}
Running results :
Construct The process :
ParentMember Construct
Parent Construct
ChildMember Construct
Child Construct
Destruct The process :
Parent Destruct
ParentMember Destruct
It's not hard to see from the running results , In the process of construction , The order of execution is :
- First base class , Post derivation ;
- Base class first member 、 Post constructor body ;
- Derived class first member 、 Post constructor body ;
- The overall call sequence is : Members of the base class -> The constructor body of the base class -> Members of derived classes -> Constructor body of derived class ;
- If breakpoint debugging , Will find , First run to the subclass constructor '{' It's about , You will call the base class member variable first .
Destructor execution order did not execute as expected , See : Example 2 The tail details . Revise the plan , See : Example 4.
1.4. Example 4: comprehensive : Base class 、 Derived class virtual destructor (virtual Virtual functions ) Call to order The optimized scheme
class Parent
{
public:
Parent()
{
std::cout<<"Parent Construct"<< std::endl;
}
virtual ~Parent() // increase virtual
{
std::cout<<"Parent Destruct"<< std::endl;
}
private:
ParentMember parentMember;
};
Running results :
Construct The process :
ParentMember Construct
Parent Construct
ChildMember Construct
Child Construct
Destruct The process :
Child Destruct
//ChildMember Destruct // No output ???
Parent Destruct
ParentMember Destruct
It is not difficult to see the following rules :
C++ The calling order of the constructor is :
Members of the base class -> The constructor body of the base class -> Members of derived classes -> Constructor body of derived class
C++ The order in which destructors are called is :
Destructors of derived classes -> Member destructors of derived classes -> The destructor of the base class -> Member destructor of base class
边栏推荐
- 服务器常用的一些硬件信息(不断更新)
- 重新理解oauth2.0协议进行联合登录
- EMC-浪涌
- R语言ggplot2可视化:使用ggplot2可视化散点图、在geom_point参数中设置alpha参数指定数据点的透明度级别(points transparent、从0到1)
- Is the golden cycle of domestic databases coming?
- 缓存雪崩和缓存穿透解决方案
- 1175. 质数排列
- 使用cookie技术实现历史浏览记录并控制显示的个数
- R language ggplot2 visualization: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)
- wallys/3×3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise
猜你喜欢

再不上市,旷视科技就熬不住了

MySQL 表的内连和外连

限时预约|6 月 Apache Pulsar 中文开发者与用户组会议

It is said that with this, the boss opened the test overnight

koa - 洋葱模型浅析

R language view version R package view version

Paper interpretation (AGC) attributed graph clustering via adaptive graph revolution

The sci-fi ideas in these movies have been realized by AI

A theoretical defect of relative position coding transformer and Its Countermeasures

优惠券种类那么多,先区分清楚再薅羊毛!
随机推荐
一瓶水引发的“战争”
Summer vacation study record
led背光板的作用是什么呢?
Dameng data rushes to the scientific innovation board, or becomes the "first share of domestic database" in the A-share market
这些电影中的科幻构想,已经用AI实现了
STM32F407ZGT6使用SDIO方式驱动SD卡
Is the golden cycle of domestic databases coming?
科普达人丨漫画图解什么是eRDMA?
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用labs函数为动画图添加动态时间标题(抽取frame_time信息)
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ The size function configures the measurement adjustment range of the size of the data point
EMC surge
Lucene full text search toolkit learning notes summary
Automatic database growth
wallys/3×3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise
R language ggplot2 visualization: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)
1175. prime permutation
又被 Kotlin 语法糖坑惨的一天
Esp32-c3 introductory tutorial IOT part ⑤ - Alibaba cloud Internet of things platform espaliyun RGB LED practical mass production solution
Database transactions
Alibaba cloud database represented by polardb ranks first in the world