草庐IT

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

feixianxing 2023-04-17 原文

前言

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

案例背景

这篇文章大致是构造了如下的一个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." <<endl;
    // 为指针分配内存
    ptr = new int;
    *ptr = len;
}
  • 拷贝构造函数:
Line::Line(const Line &obj){
    cout<< "Copy constructor allocating ptr." <<endl;
    ptr = new int;
    // copy the value
    //这里右式的运算顺序是先获取obj.ptr,再用'*'取值.
    //因为是复制值,而不是复制地址,所以'='两边都要加上'*',
    //否则,多个Line对象的长度都会被绑定到一起。
    *ptr = *obj.ptr;
}
  • 析构函数(在对象被销毁时执行):
Line::~Line(void){
    cout<< "Freeing memory!"<<endl;
    delete ptr;
}
  • 获取Line对象的长度,直接返回指针指向的int类型数据
int Line::getLength(void){
    return *ptr;
}
  • 定义一个display函数,用于输出Line对象的长度:
void display(Line obj){
    cout<< "Length of line : "<<obj.getLength() <<endl;
}

正文

对于以下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++]关于拷贝构造函数和析构函数执行次数的思考的更多相关文章

  1. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  2. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  3. ruby - Chef 执行非顺序配方 - 2

    我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul

  4. ruby-on-rails - 在 ruby​​ 中使用 gsub 函数替换单词 - 2

    我正在尝试用ruby​​中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了

  5. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  6. ruby - 为什么 Ruby 的 each 迭代器先执行? - 2

    我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试

  7. ruby - 检查是否通过 require 执行或导入了 Ruby 程序 - 2

    如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby​​文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否

  8. ruby - 在 Ruby 中按名称传递函数 - 2

    如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只

  9. postman——集合——执行集合——测试脚本——pm对象简单示例02 - 2

    //1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json

  10. ruby-on-rails - rbenv:从 RVM 移动到 rbenv 后,在 Jenkins 执行 shell 中找不到命令 - 2

    我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions

随机推荐