I read about references and objects that are initialised in a class. However, I couldn't find a clear statement, except for hints, to the following question:
Can I initialise a member object obj1
in an initialisation list if it doesn't have a default constructor (no Object(){}
)?
class Sample
{
private:
Object1 obj1(arguments);
public:
Sample(Object1 o1) : obj1( o1(arguments) )
{ }
};
The question came up, because if have a problem related to this How can I initialize C++ object member variables in the constructor?. The code is taken form there as well. Thanks for your effort.
Daniel
EDIT:
Since the answer suggest that it works, a test returned an error (which is exactly the reason I ask this question):
../src/Timestep.h:45:12: error: field ‘myFEMSolver’ has incomplete type FEMSolver myFEMSolver;
Code:
class Timestep {
public:
Timestep();
private:
FEMSolver myFEMSolver;
}
Timestep::Timestep() : myFEMSolver(*this)
{ //do some stuff
}
FEMSolver::FEMSolver(const Timestep& theTimestep) : myTimestep(theTimestep)
{ //do some stuff
}
main(){
Timestep myTimestep();
}