Member variable as reference

What is the advantage of declaring a member variable as a reference? I saw how people did this, and do not understand why.

+3
source share
3 answers

One useful case is when you do not have access to the constructor of this object, but you do not want to work with indirection through a pointer. For example, if a class Adoes not have an open constructor, and your class wants to accept an instance Ain its constructor, you want to save A&. It also guarantees link initialization.

+4
source

An element reference is useful when you need to have access to another object without copying it.

(), .

+1

, , std::auto_ptr<> ++, ( ).

, , operator= , , , , . , .

, .

+1

Source: https://habr.com/ru/post/1795168/


All Articles