(大卫的阅读笔记)关于对象的construct与destruct

大卫注 :
今天整理Computer上的旧资料 ,偶然发现了两篇不知什么时候从网上下来的文章 ,谈的是关于对象construct与destruct的问题 ,感觉作者讲的不是太清楚 ,按其思想进行改写后发布于此 .

对象的construct与destruct是C ++中一个最基本的概念 ,虽然简单 ,但其中有些特性也值得我们去关注 ,以便更好地加以利用 ,写出有效而且高效的代码 .
先看一个程序 ,程序很简单 ,只是添加了一些输出信息让它看起来内容有点多 :
// Demonstrates returning a temporary object.
#include <iostream>
using namespace std ;

// class to print construct/destruct information
class CDemo
{

public
:
    CDemo ( const CDemo  & rcd )
    {

        cout  <<  "Copy constructor: "  << &rcd  <<  "->"  <<  this  << endl ;
    }


    CDemo ()
    {

        cout  <<  "Default constructor: "  <<  this  << endl ;
    }


    CDemo  & operator =( const CDemo  & rcd )
    {

        cout  <<  "Assignment operator: "  << &rcd  <<  "->"  <<  this  << endl ;
        return
 * this ;
    }


    CDemo ::~CDemo ()
    {

        cout  <<  "Destructor: "  <<  this  << endl ;
    }
};


// demo function to use CDemo and trigger something under the hood
CDemo foo ()
{

    cout  <<  "In foo"  << endl ;
    CDemo cd ;
    cout  <<  "cd is: "  << &cd  << endl ;
    // Return a copy of cd.
    return cd ;
}


int
 main ()
{

    // This code generates a temporary
    // object, which is copied
    // into cd2 using the assignment
    // operator.
    CDemo cd2 ;
    cd2  = foo ();
    cout  <<  "cd2 is: "  << &cd2  << endl  << endl ;

    // This code generates the temporary
    // object directly in the location
    // of retval;
    CDemo cd1  = foo ();
    cout  <<  "cd1 is: "  << &cd1  << endl  << endl ;

    const
 CDemo & rcd  = foo ();
    cout  <<  "Return from main!"  << endl ;

    return
 0 ;
}

以下是程序的输出 :
Default constructor : 0012FF6C
In foo
Default constructor : 0012FEEC
cd is : 0012FEEC
Copy constructor : 0012FEEC ->0012FF60
Destructor : 0012FEEC
Assignment  operator : 0012FF60 ->0012FF6C
Destructor : 0012FF60
cd2 is : 0012FF6C

In foo
Default constructor : 0012FEEC
cd is : 0012FEEC
Copy constructor : 0012FEEC ->0012FF70
Destructor : 0012FEEC
cd1 is : 0012FF70

In foo
Default constructor : 0012FEEC
cd is : 0012FEEC
Copy constructor : 0012FEEC ->0012FF64
Destructor : 0012FEEC

Return from  main !
Destructor : 0012FF64
Destructor : 0012FF6C
Destructor : 0012FF70

在上面的程序中的 main函数中 ,我们以不同方式定义了两个CDemo对象和一个CDemo对象的引用 ,显然 ,由于使用的方式不同 ,上面的输出存在较大的区别 .
下面逐一对以上 3组输出进行分析 ,请注意输出信息中的地址信息 .
1.

CDemo cd2 ;  // 默认构造
cd2  = foo ();  // 依次经历一次默认构造,一次拷贝构造(构造返回时的临时对象),一次赋值
2.
CDemo cd1  = foo ();  // 经历了一次构造和一次拷贝构造过程,这里好像不存在拷贝构造返回的临时对象的过程,其实并非如此.由于编译器的优化,对象被直接构造在了cd1的缓冲区上.
3.
const
 CDemo & rcd  = foo ();  // 这里同样也存在拷贝构造返回时的临时对象的过程,但是,与1中不同的是,该临时对象没有马上释放,而是直到main函数返回时才释放.这是为什么呢?其实,这是由const reference的特性导致的,C++标准规定,如果一个临时对象被赋值给一个(常量)引用,这个临时对象在这个(常量)引用的生命周期中将不能被销毁(C++标准只规定了对const reference是这样的,对于普通的reference,虽然可能也是如此,但并不安全).
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值