For a scientific code I need to construct classes that require (member) pointers to member variables of other classes (i.e. both parents and siblings).
I have researched this for hours and found several topics about pointers to member variables, but nothing about my specific problem. Hence my question: what is the correct syntax to instantiate class C in the example below such that the pointer in class C points to the VectorXd beta in class B?
Edit: To clarify my question, the intended behavior is that a call to the class B member function PrintBetas() prints out the current value of VectorXd beta nr_C times.
#include <iostream>
#include <vector>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
class C {
public:
C( VectorXd *beta_IN ): beta( beta_IN ) {}
void PrintBeta() {
cout << "(*beta) = " << (*beta).transpose() << endl; // ???
}
private:
const VectorXd *beta;
};
class B {
public:
B( VectorXd beta_IN, int nr_C ): beta( beta_IN ) {
for( auto i=0; i<nr_C; i++)
vec.push_back( C( &beta ) );
}
void PrintBetas() {
for (auto c : vec)
c.PrintBeta();
}
VectorXd beta;
private:
vector<C> vec;
};
class A {
public:
A( int nr_B, int nr_C ) {
for (auto i=0; i<nr_B; i++) {
VectorXd beta = VectorXd::Random(3);
subclass.push_back( B( beta, nr_C) );
}
}
vector<B> subclass;
};
int main() {
A test(2,3);
test.subclass[0].PrintBetas();
test.subclass[1].beta.setZero();
test.subclass[1].PrintBetas();
}