c++ - Howto initialize a reference to an object if reference is a class member? -


let's class contains reference called matrix_:

class.h

class class { matrix& matrix_; } 

class.cpp

class::class() : matrix_(matrix()) { } 

i error: invalid initialization of non-const reference of type ‘matrix&’ temporary of type ‘matrix’.

i see problem temporary object disappear , reference point null. how can create persistent object reference? want use reference because member should constant.

class::class() : matrix_(matrix()) tries set reference point temporary object, illegal.

well, there's case const references , temporary binding, seriously, don't go there.

looks need use aggregation:

class class { const matrix matrix_; }; 

and initializer list:

class::class() : matrix_() /* or params constructor if need them */ { } 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -