C++探讨 一: 函数返回值为类对象和类对象引用

1. 函数返回类对象

在C++中,定义一个函数时可以返回类的对象。本段介绍在返回类的对象过程中,C++都做了什么? 这个行为取决于你的代码是如何书写的。有可能会直接调用类的构造函数,也有可能调用拷贝构造函数。下面为分析

1.1. 调用构造函数

先看一段代码:

#include <iostream>
using namespace std;

class Car
{
public:
    Car() { cout << "Constructor Car." << endl; }
    Car(const Car& rh) { cout << "Copy Constructor Car." << endl; }
    ~Car() { cout << "Destructor Car." << endl; }
    void PrintInfo() { cout << "I am a Car" << endl; }
};

Car CarFactory()
{
    cout << "This is in car factory" << endl;
    return Car();
}


int main()
{
    Car lCar = CarFactory();
    lCar.PrintInfo();
    return 0;
}

运行结果:
在这里插入图片描述
在函数 CarFactory() 中, 返回一个即时构造的对象,直接调用构造函数 并将其赋值给lCar. 最后main函数执行完进行析构。

1.2. 调用拷贝构造函数

先看一段代码:

#include <iostream>
using namespace std;

class Car
{
public:
    Car() { cout << "Constructor Car." << endl; }
    Car(const Car& rh) { cout << "Copy Constructor Car." << endl; }
    ~Car() { cout << "Destructor Car." << endl; }
    void PrintInfo() { cout << "I am a Car" << endl; }
};

Car CarFactory()
{
    Car lCar;
    cout << "This is in car factory" << endl;
    return lCar;
}


int main()
{
    CarFactory();
    return 0;
}

输出结果:
在这里插入图片描述
在函数 CarFactory() 中, 返回一个局部变量 lCar。 程序执行顺序如下:

  1. lCar 构造函数
  2. lCar 拷贝构造函数,被用来当做函数的返回。
  3. lCar 析构函数
  4. main函数中新的Car对象(函数返回的对象)进行析构

该结果有争议!!! 将上述代码在不同环境中进行测试,得出的结论不同。我测试三个环境, 在两个不同的Windows环境和一个Macbook环境上编译运行,结果只有一个Windows环境和上述结论相同。 另外两个环境(Windows 和 Mac)均不调用拷贝构造函数。这个可能与编译器和运行环境有关。

2. 函数返回类对象的引用

2.1. 返回正常引用

先来一段代码

#include <iostream>
using namespace std;

class Car
{
public:
    Car(int id):mId(id) { cout << "Constructor Car." << endl; }
    void PrintInfo() { cout << "I am a Car: " << mId << endl; }
private:
    int mId;
};

Car gCarArray[] = {Car(0), Car(1), Car(2), Car(3)};

Car& CarFactory(int index)
{
    if (index < 0 || index >= sizeof gCarArray / sizeof Car)
        throw "Invalid Index";

    return gCarArray[index];
}

int main()
{
    Car& lCar = CarFactory(3);
    lCar.PrintInfo();
    return 0;
}

上述代码返回一个正常的引用,返回的过程中没有新的构造函数或析构函数的调用。

2.2. 返回局部对象的引用: 未定义行为(✖✖✖!! Undefined Behavior)

先来一段代码:

#include <iostream>
using namespace std;

class Car
{
public:
    Car() { cout << "Constructor Car." << endl; }
    Car(const Car& rh) { cout << "Copy Constructor Car." << endl; }
    ~Car() { cout << "Destructor Car." << endl; }
    void PrintInfo() { cout << "I am a Car" << endl; }
};

Car& CarFactory()
{
    Car lCar;
    cout << "This is in car factory" << endl;
    return lCar;
}


int main()
{
    Car& lMainCar = CarFactory();
    lMainCar.PrintInfo();
    return 0;
}

仔细看, 这里返回个局部变量的引用lCar。 在我的编译器里,编译通过。 但是有警告:

Warning C4172 returning address of local variable or temporary: lCar

最终运行结果:
在这里插入图片描述
从运行结果上来分析,很诡异。 这是属于未定义的行为。 在第三行中,Car的析构函数已经被调用了,但是最后仍然能调用PrintInfo() 打印相关信息。 这里能打印出来的原因可能是编译器处理的缘故,但是这种行为是未定义的行为。以后不要写这种未定义行为类似的代码。

  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值