Reputation: 979
I want to declare two objects from two different classes inside my private section of my class. The problem I have is that the second object should take the first object in a constructor. So here is the example of my class private section:
class FactorGraph
{
private:
gtsam::ISAM2Params _parameters();
gtsam::ISAM2 _isam(_parameters);
The _parameters object should be passed as an argument for the _isam object but from my knowledge that will not be possible as C++ does not allow this. Is there a slick way to do so?
Upvotes: 2
Views: 107
Reputation: 11570
Initialize your second object in the constructor (or better both):
FactorGraph::FactorGraph()
: _parameters{}, _isam{_parameters}
{ }
The initialization happens in the same order in which the members are listed in your class (regardless of the order of this list(!), so it's a good idea to keep the same order here, your compiler may even warn you otherwise), which here guarantees that _isam
won't get anything uninitialized. (I'm not aware of such guarantees if you just define your objects in your class declaration.)
From the comments, thanks @drescherjm: In your class, just declare your objects without initializers:
class FactorGraph
{
private:
gtsam::ISAM2Params _parameters;
gtsam::ISAM2 _isam;
When you declare them without initializers, they should be initialized in the constructor instead. They do not need to be default-constructible in order to do this, the objects gets initialized only once (in the constructor)
Then if you have more constructors, don't forget to either do that everywhere or forward to this one:
FactorGraph(int) : FactorGraph{}, /* other initializers */ { }
Upvotes: 4