I am working on the C++ framework OpenFOAM, more especially on a library for a project. I can't manage to declare the constructor of my class the way I want. I think my question is interesting from a C++ point of view.
Here is my constructor (in arbMesh.C):
explicit arbMesh(volScalarField& Rho)
:
rho_(Rho),
mesh_(Rho.mesh())
{}
"Rho" is a volScalarField reference, "mesh_" is a const fvMesh reference.
What I want to do is add another attribute of type "pointMesh" that is defined from a "fvMesh" object. My direct idea was to add the attribute "const pointMesh& pMesh" attribute to the arbMesh class and the above was then changed to:
explicit arbMesh(volScalarField& Rho)
:
rho_(Rho),
mesh_(Rho.mesh()),
pMesh_(pointMesh::New(Rho.mesh()))
{}
or
explicit arbMesh(volScalarField& Rho)
:
rho_(Rho),
mesh_(Rho.mesh()),
pMesh_(pointMesh::New(mesh_))
{}
or
explicit arbMesh(volScalarField& Rho)
:
rho_(Rho),
mesh_(Rho.mesh()),
pMesh_(const fvMesh& mesh_)
{}
Obviously none of these solutions work but I think I am getting close. The problem is that initialization is not done correctly. Indeed I recall those are references, they HAVE to be declared like:
class arbMesh
{
volScalarField& rho_;
const fvMesh& mesh_;
const pointMesh& pMesh_;
...
Here is the compilation error for the 3rd option:
arbMesh.H.:97:11: error: expected primary-expression before 'const'
pMesh_(const fvMesh mesh_)
How do I declare the pMesh so that the it is initialized correctly ?
Please browse the source code of OpenFOAM if you are not familiar with its classes. I have not been able to find a suitable answer on c++ forums nor in the OF community.