我已经阅读了很多关于返回值优化的文章。但是我不确定是否完全理解在以下情况下是否会发生这种情况(地址实际上总是相同的):
#include "stdafx.h"
class Type
{
public:
Type(int a) { m_a = a; }
private:
Type(const Type & other) {}
int m_a;
};
class Base
{
public:
Base() : instance(42) {}
virtual Type & GetType()
{
printf("Base: Address of instance: %i\n", &instance);
return instance;
}
private:
Type instance;
};
class Derived : public Base
{
public:
virtual Type & GetType()
{
printf("Derived\n");
return Base::GetType();
}
};
int main()
{
printf("Base class\n");
Base b;
printf("Main: Address of instance: %i\n", &(b.GetType()));
printf("\nDerived class\n");
Derived d;
printf("Main: Address of instance: %i\n", &(d.GetType()));
}
按引用返回是否始终确保不调用复制构造函数?
或者RVO是否在这里进行?