Imagine that we have a non-temporary int
value which is referenced by an instance of another class Foo
:
class Foo
{
public:
Foo(const int& reference) :
mReference(reference)
{
}
private:
const int& mReference;
};
Instances of Foo
should reference this int
value but not copy it.
Is it possible to somehow define the constructor argument to avoid a possible accidental copying?
class Foo
{
public:
Foo(const int& reference) :
mReference(reference) // We need to somehow prevent this copying
{
}
private:
// The member is accidentally defined not as const int& but as int
int mReference;
};
Is it any std-way to do this, something like std::unique_ptr
but for references and without dynamic memory allocation?