0

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.

Dash
  • 1
  • 1
  • 1
    "Obviously none of these solutions work but I think I am getting close" maybe its me, but I dont think there is anything obvious. Do you get compiler errors? If yes, include them in the question – 463035818_is_not_an_ai May 10 '19 at 14:22
  • Are you sure you don't just want a `pointMesh pMesh_;` member? – François Andrieux May 10 '19 at 14:23
  • You should take note of the [disadvantages of using reference type data members](https://stackoverflow.com/questions/892133/should-i-prefer-pointers-or-references-in-member-data). – François Andrieux May 10 '19 at 14:24
  • I know guys it is better to NOT use references, unfortunately it is a requirement at the moment.. I have added my last compilation error – Dash May 10 '19 at 14:26
  • Pleasea include also a [mcve]. The last version of your code is rather bogus and I have no idea what `pMesh_(const fvMesh& mesh_)` is supposed to mean in the initializer list, so that error message alone is not sufficient – 463035818_is_not_an_ai May 10 '19 at 14:31
  • 2
    do I understand correctly that you want to create the `pMesh_` in the constructor? In that case you really need to explain why the member needs to be a reference. You cannot have a reference without storing the object itself somewhere – 463035818_is_not_an_ai May 10 '19 at 14:33
  • For test purpose I can implement it not as a reference. So if I just change the declaration of the pMesh attribute to "pointMesh pMesh_;" I get an error "field 'oMesh_' has incomplete type 'Foam::pointMesh'". It has to be because pointMesh cannot be declare with a null constructor. It requires at least a "fvMesh" object. The problem is that this fvMesh is given at runtime.. – Dash May 10 '19 at 15:16
  • 1
    The incomplete type error means that the class definition is missing (its respective header is not included, but it was forward declared in one of your other includes). Pointer and reference members can be incomplete types, but to store an object by value or use any kind of member access on a reference/pointer, you need the full class definition. I suggest you have a look at this if you are not familiar with the concept: https://en.cppreference.com/w/cpp/language/type#Incomplete_type – Balázs Kovacsics May 10 '19 at 15:44
  • "What I want to do is add another attribute of type "pointMesh" that is defined from a "fvMesh" object." what do you mean? pMesh_ is gonna be an output of a fvMesh method? – alexmogavero May 14 '19 at 19:33

0 Answers0