C++ 拷贝构造(一)

如果在一个类Class A的定义中有一个构造函数,这个构造函数的参数是这个A 类型的const 的reference(引用),这个构造函数就可以
在做初始化的时候(A(const A& x))被调用。当定义的类里没有做这类构造函数,而在程序中,又有类似于

A a;
A a2 = a;

这种情况时,编译器会默认自己有一个A(const A& x) 的构造函数。

代码一:

#include <iostream>
#include <string>
using namespace std;

static int objectCount = 0;

class HowMany {
    public:
        HowMany() {
            objectCount++;
            print("HowMany()");
        }
        HowMany(int i){
            objectCount ++;
            print("HowMany(int)");
        }
        void print(const string& msg ="") {
            if(msg.size()!=0) {
                cout<<msg<<": "<<endl;
            }
            cout<<"objectCount = "<<objectCount<<endl;
        }
        ~HowMany(){
            objectCount--;
            print("~HowMany()");
        }
};
HowMany f(HowMany x){
    cout<<"begin of f"<<endl;
    x.print("x argument inside f()");
    cout<<"end of f"<<endl;
    return x;
}

int main() 
{
    HowMany h;
    h.print("after construction of h");
    HowMany h2 = h;
    h.print("after call to f()");
    return 0;
}

在代码一中,在类内没有定义一个类似于A(const A& x) 的构造函数,而在程序中又有HowMany h2 = h;,所以编译器默认生成一个 HowMany(const HowMany& x) 的构造函数。所以输出的内容是

HowMany(): objectCount = 1
after construction of h: objectCount = 1
after call to f(): objectCount = 1
~HowMany(): objectCount = 0
~HowMany(): objectCount = -1

--------------------------------
Process exited after 0.4916 seconds with return value 0
请按任意键继续. . .

即第一个HowMany是h调用的,而h2调用的是编译器默认的构造函数,最终有两个析构函数。

代码二:

#include <iostream>
#include <string>
using namespace std;

static int objectCount = 0;

class HowMany {
    public:
        HowMany() {
            objectCount++;
            print("HowMany()");
        }
        HowMany(int i){
            objectCount ++;
            print("HowMany(int)");
        }
        HowMany(const HowMany& x){
            objectCount ++;
            print("HowMany(HM)");
        }
        void print(const string& msg ="") {
            if(msg.size()!=0) {
                cout<<msg<<": ";
            }
            cout<<"objectCount = "<<objectCount<<endl;
        }
        ~HowMany(){
            objectCount--;
            print("~HowMany()");
        }
};
HowMany f(HowMany x){
    cout<<"begin of f"<<endl;
    x.print("x argument inside f()");
    cout<<"end of f"<<endl;
    return x;
}

int main() 
{
    HowMany h;
    h.print("after construction of h");
    HowMany h2 = h;
    h.print("after call to f()");
    return 0;
}

而代码二则在类中自己定义了类似A(const A& x) 的构造函数。所以结果如下:

HowMany(): objectCount = 1
after construction of h: objectCount = 1
HowMany(HM): objectCount = 2
after call to f(): objectCount = 2
~HowMany(): objectCount = 1
~HowMany(): objectCount = 0

--------------------------------
Process exited after 0.4221 seconds with return value 0
请按任意键继续. . .

拷贝构造函数在以下几种情况下会被调用。

1.当用类的一个对象初始化该类的另一个对象时.例如:

int main()
{
   point A(1,2);
   point B(A);//用对象A初始化对象B,拷贝构造函数被调用.
}

2.如果函数的形参是类的对象,调用函数时,进行形参和实参结合时.

void f(point p)
{
}
main()
{
   point A(1,2);
   f(A);//函数的形参为类的对象时,当调用函数时,拷贝构造函数被调用.
}

3.如果函数的返回值是类的对象,函数执行完成返回调用者时.

point g()
{
   point A(1,2);
   return A;//函数的返回值是类的对象,返回函数值时,调用拷贝构造函数.
}
void main()
{  
   point B;
   B = g();
}

4、需要产生一个临时类对象时。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值