I have read a lot of articles about return value optimization. Yet I'm not sure to fully understand if this is what takes place in the following case (the addresses are actually always identical):
#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()));
}
Does returning by reference always ensure no copy constructor is called?
Or is RVO taking place here?