c++拷贝构造函数详解

 

//
//  main.cpp
//  C++Test31
//

#include <iostream>
using namespace std;
//C++拷贝构造函数详解
class CExample
{
private:
    int a;
public:
    //构造函数
    CExample(int b)
    {
        a=b;
        printf("constructor is called\n");
    }
    //拷贝构造函数
    CExample(const CExample & c)
    {
        a=c.a;
        printf("copy constructor is called\n");
    }
    //析构函数
    ~CExample()
    {
        cout<<"destructor is called\n";
    }
    void Show()
    {
        cout<<a<<endl;
    }
};

void g_fun(CExample c)
{
    cout<<"g_func"<<endl;
}

CExample g_fun1() {
    CExample temp(0);
    cout<<"g_func1"<<endl;
    return temp;
}

int main(int argc, const char * argv[]) {
    CExample A(100);
    CExample B=A;
    B.Show();
//    g_fun(A);
    
    g_fun1();
    return 0;
}

//constructor is called
//copy constructor is called
//100
//destructor is called
//destructor is called

//constructor is called
//copy constructor is called
//100
//copy constructor is called
//g_func
//destructor is called
//destructor is called
//destructor is called

//constructor is called
//copy constructor is called
//100
//constructor is called
//g_func1
//destructor is called
//destructor is called
//destructor is called

//
//  main.cpp
//  C++Test32
//

#include <iostream>
using namespace std;
class Rect
{
public:
    Rect()
    {
        count++;
    }
    ~Rect()
    {
        count--;
    }
    static int getCount()
    {
        return count;
    }
private:
    int width;
    int height;
    static int count;
};
int Rect::count=0;

int main(int argc, const char * argv[]) {
    Rect rect1;
    cout<<"The count of Rect:"<<Rect::getCount()<<endl;
    Rect rect2(rect1);
    cout<<"The count of Rect:"<<Rect::getCount()<<endl;
    return 0;
}

//The count of Rect:1
//The count of Rect:1

//
//  main.cpp
//  C++Test33
//

#include <iostream>
using namespace std;
class Rect
{
public:
    Rect()
    {
        count++;
    }
    Rect(const Rect& r)
    {
        width=r.width;
        height=r.height;
        count++;
    }
    ~Rect()
    {
        count--;
    }
    static int getCount()
    {
        return count;
    }
private:
    int width;
    int height;
    static int count;
};
int Rect::count=0;

int main(int argc, const char * argv[]) {
    Rect rect1;
    cout<<"The count of Rect:"<<Rect::getCount()<<endl;
    Rect rect2(rect1);
    cout<<"The count of Rect:"<<Rect::getCount()<<endl;
    return 0;
}

//The count of Rect:1
//The count of Rect:2


//
//  main.cpp
//  C++Test34
//

#include <iostream>
// 浅拷贝
using namespace std;
class Rect
{
public:
    Rect()
    {
        p=new int(100);
    }
    Rect(const Rect& r)
    {
        width=r.width;
        height=r.height;
        p = new int(100);
        *p = *(r.p);
    }
    ~Rect()
    {
        assert(p!=NULL);
        delete p;
    }
public:
    int width;
    int height;
    int *p;
};

int main(int argc, const char * argv[]) {
    Rect rect1;
    rect1.width = 100;
    cout << rect1.width << endl;
    Rect rect2(rect1);
    cout << rect2.width << endl;
    return 0;
}


//
//  main.cpp
//  C++Test35
//

#include <iostream>
using namespace std;
//防止按值传递
class CExample
{
public:
    //构造函数
    CExample(int b)
    {
        a = b;
        cout<<"creat: "<<a<<endl;
    }
  
private:
    //拷贝构造函数,只是声明,可以申明多个,由编译器根据上下文决定选择哪一个
    CExample(const CExample& C);
    CExample(CExample& C);

public:
    ~CExample()
    {
        cout<< "delete: "<<a<<endl;
    }
    
    void Show ()
    {
        cout<<a<<endl;
    }
    
private:
    int a;
};

void g_Fun(CExample C)
{
    cout<<"c"<<endl;
}

int main(int argc, const char * argv[]) {
    CExample c(1);
//    g_Fun(c);   //按值传递将出错 Calling a private constructor of class 'CExample'
    
    return 0;
}

参考https://www.cnblogs.com/alantu2018/p/8459250.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

VCHH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值