C++引用的一些想法

C++引用的一些想法

用不用调用copy constructor?

直接上源码和测试结果

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

class Test{
private:
    int value;
public:
    Test(int value_) : value(value_){
        cout << "Here is constructor" << endl;
    }
    ~Test(){
        cout << "Here is destructor" << endl;
    }
    Test(Test &t){
        value = t.value;
        cout << "Here is copy constructor" << endl;
    }
    void MemberFun1(){
        cout << "Here is MemberFun1" << endl;
        value++;
    }
    Test MemberFun2(){
        cout << "Here is MemberFun2" << endl;
        return (*this);
    }
    Test MemberFun3(){
        cout << "Here is MemberFun3" << endl;
        Test& ret = (*this);
        return ret;
    }
    Test& MemberFun0(){
        cout << "Here is MemberFun0" << endl;
        Test& ret = (*this);
        return ret;
    }
    Test MemberFun4(Test t){
        cout << "Here is MemberFun4" << endl;
        value += t.value;
        return t;
    }
    Test MemberFun5(Test &t){
        cout << "Here is MemberFun5" << endl;
        value += t.value;
        return t;
    }
    Test& MemberFun6(Test &t){
        cout << "Here is MemberFun6" << endl;
        value += t.value;
        return t;
    }
};

void GlobalFun1(Test t){
    cout << "Here is GlobalFun1" << endl;
}

Test GlobalFun2(Test t){
    cout << "Here is GlobalFun2" << endl;
    return t;
}

void GlobalFun3(Test &t){
    cout << "Here is GlobalFun3" << endl;
}

Test GlobalFun4(Test &t){
    cout << "Here is GlobalFun4" << endl;
    return t;
}


int main(){
    Test t(1);
    Test g(2);
    cout << "In MemberFun:" << endl;
    t.MemberFun1();
    cout << endl;
    t.MemberFun2();
    cout << endl;
    t.MemberFun3();
    cout << endl;
    t.MemberFun0();
    cout << endl;
    t.MemberFun4(g);
    cout << endl;
    t.MemberFun5(g);
    cout << endl;
    t.MemberFun6(g);
    cout << endl << endl;
    cout << "In GlobalFun:" << endl;
    GlobalFun1(t);
    cout << endl;
    GlobalFun2(t);
    cout << endl;
    Test& rt = t;
    GlobalFun1(rt);
    cout << endl;
    GlobalFun2(rt);
    cout << endl;
    GlobalFun3(rt);
    cout << endl;
    GlobalFun4(rt);
    cout << endl;
    return 0;
}


输出如下:

Here is constructor
Here is constructor
In MemberFun:
Here is MemberFun1

Here is MemberFun2
Here is copy constructor
Here is destructor

Here is MemberFun3
Here is copy constructor
Here is destructor

Here is MemberFun0

Here is copy constructor
Here is MemberFun4
Here is copy constructor
Here is destructor
Here is destructor

Here is MemberFun5
Here is copy constructor
Here is destructor

Here is MemberFun6


In GlobalFun:
Here is copy constructor
Here is GlobalFun1
Here is destructor

Here is copy constructor
Here is GlobalFun2
Here is copy constructor
Here is destructor
Here is destructor

Here is copy constructor
Here is GlobalFun1
Here is destructor

Here is copy constructor
Here is GlobalFun2
Here is copy constructor
Here is destructor
Here is destructor

Here is GlobalFun3

Here is GlobalFun4
Here is copy constructor
Here is destructor

Here is destructor
Here is destructor

总结:
对于类成员函数而言:
1. 参数表为this,调用函数不会调用拷贝构造
2. 当且仅当生命类成员函数返回为声明且确实返回声明的时候,不会调用拷贝构造。
3. 当且仅当传参表声明为引用时,传入参数时不会调用拷贝构造。
对全局函数而言:
1. 与外部传入的是否为引用无关,与参数表声明有关,当参数表声明为引用时,不会调用拷贝构造。

时间

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

class Test{
private:
    int value;
public:
    Test(int value_) : value(value_){}
    ~Test(){}
    Test(Test &t){
        value = t.value;
    }
    void MemberFun(){}
};

void GlobalFun0(Test t){}

void GlobalFun1(Test &rt){}

void GlobalFun2(Test *pt){}

int main(){
    clock_t startTime, endTime;
    Test t(1);
    Test* pt = &t;
    Test& rt = t;
    cout << "In GlobalFun" << endl;
    startTime = clock();
    for(int i = 0; i < 10000000; i++){
        GlobalFun0(t);
    }
    endTime = clock();
    cout << "The run time (pass object) is: " <<(double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
    startTime = clock();
    for(int i = 0; i < 10000000; i++){
        GlobalFun1(rt);
    }
    endTime = clock();
    cout << "The run time (pass reference) is: " <<(double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
    startTime = clock();
    for(int i = 0; i < 10000000; i++){
        GlobalFun2(pt);
    }
    endTime = clock();
    cout << "The run time (pass pointer) is: " <<(double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
    cout << endl << "In MemberFun" << endl;
    for(int i = 0; i < 10000000; i++){
        t.MemberFun();
    }
    endTime = clock();
    cout << "The run time (pass object) is: " <<(double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
    startTime = clock();
    for(int i = 0; i < 10000000; i++){
        rt.MemberFun();
    }
    endTime = clock();
    cout << "The run time (pass reference) is: " <<(double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
    startTime = clock();
    for(int i = 0; i < 10000000; i++){
        pt->MemberFun();
    }
    endTime = clock();
    cout << "The run time (pass pointer) is: " <<(double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
    return 0;
}

指针和引用速度几乎相同,比直接传递对象要快。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值