I wish to pass by reference my data to a class where the reference itself should be the public variable, to be accessed by member functions. If I declare my class and its constructor
class max_likelihood {
public:
MatrixXd dat
max_likelihood(const Ref<const MatrixXd>& dat_in)
{dat = dat_in;}
I get functioning code but end up creating a copy of dat
, which I would like to avoid.
I have tried to do instead:
class max_likelihood {
public:
const Ref<const MatrixXd>& dat;
max_likelihood(const Ref<const MatrixXd>& dat){}
But this does not work and/or won't let me access the reference to dat
and does not even compile.
Based on my research I have found this bit from this question
if you want to reassign a Ref to reference another buffer, then use a placement new to re-call the constructor of Ref. Don't forget to call the destructor first.
I believe this may help answer my question but I do not know what these instructions would mean in practice, hence my question here. Specifically, I suppose I am creating a new instance of a Ref object to pass around the passed reference. How can I interpret the answer to this or find a more elegant way to use Ref
objects within classes when the source data is created elsewhere, say read in from a file via main?