资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

[c++]关于拷贝构造函数和析构函数执行次数的思考

前言

最近在学习C++的类如何构造,在W3Cschool上看到关于拷贝构造函数的一个例子,记录一下。

成都创新互联公司-专业网站定制、快速模板网站建设、高性价比南州晴隆网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式南州晴隆网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖南州晴隆地区。费用合理售后完善,十多年实体公司更值得信赖。

案例背景

这篇文章大致是构造了如下的一个Line类:

class Line{
    public:
        int getLength(void);
        Line(int len);          // 简单构造函数
        Line(const Line &obj);  // 拷贝构造函数
        ~Line();                // 析构函数

    private:
        int *ptr;   //指向length
};

其中构造函数和析构函数的定义如下:

  • 简单构造函数:
Line::Line(int len){
    cout<< "Normal constructor allocating ptr." <
  • 拷贝构造函数:
Line::Line(const Line &obj){
    cout<< "Copy constructor allocating ptr." <
  • 析构函数(在对象被销毁时执行):
Line::~Line(void){
    cout<< "Freeing memory!"<
  • 获取Line对象的长度,直接返回指针指向的int类型数据
int Line::getLength(void){
    return *ptr;
}
  • 定义一个display函数,用于输出Line对象的长度:
void display(Line obj){
    cout<< "Length of line : "<

正文

对于以下main函数的内容:

int main(){
    Line line1(10);
    Line line2(line1);  //这里调用了拷贝构造函数

    display(line1);
    display(line2);

    return 0;
}

预期的输出是:

Normal constructor allocating ptr.
Copy constructor allocating ptr.
Length of line : 10
Length of line : 10
Freeing memory!
Freeing memory!

但实际输出是:
拷贝构造函数析构函数被调用了好几次

Normal constructor allocating ptr.
Copy constructor allocating ptr.
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Freeing memory!
Freeing memory!

分析

在设置断点和调试代码之后,发现原因:

  • display函数的函数参数是值传递,也就是说在调用时会创建函数参数(Line对象)的副本,并且display函数执行完之后,副本会被删除。
  • 也就是说,每执行一次display函数,都会触发对拷贝构造函数析构函数的调用,就会输出如下的文本:
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
  • 而输出结尾的两个Freeing memory!是由于C/C++的局部变量是存储在栈区stack的。栈区由编译器自动分配和释放内存。
  • 当程序执行到return 0;的时候,局部变量line1line2被销毁,故析构函数被调用。
  • 并且需要注意的是,这两个输出的顺序是:
Freeing memory!		--> 对应line2的销毁
Freeing memory!		--> 对应line1的销毁
  • 这是因为变量是存储在栈区中的,遵循FILO(First In, Last Out)的顺序。

网站题目:[c++]关于拷贝构造函数和析构函数执行次数的思考
转载来源:http://cdkjz.cn/article/dsoigip.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220