CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   MultiRegionSetFields.C (https://www.cfd-online.com/Forums/openfoam-programming-development/176660-multiregionsetfields-c.html)

babakflame August 23, 2016 22:24

MultiRegionSetFields.C
 
Dear Fellows

I am looking at multiRegionSetFields.C file inside OF and there are some lines that I can not understand. The followings start from line 129:

Code:

class iNew
    {
        const fvMesh& mesh_;
        const labelList& selectedCells_;

    public:

        iNew(const fvMesh& mesh, const labelList& selectedCells)
        :
            mesh_(mesh),
            selectedCells_(selectedCells)
        {}

Please correct me, if I am making mistake:
In the above , the class iNew has two constant private objects (mesh_ and selectedCells_) from fvMesh and labelList classes and a constructor (iNew) which has mesh and selectedCells as input arguments. However what is that ":"
and these two lines:
Code:

:
mesh_(mesh),
selectedCells_(selectedCells)

Another Question:

Why fvMesh and labelList classes are defined as References:

Code:

const fvMesh& mesh_;
const labelList& selectedCells_;


babakflame August 24, 2016 12:17

Dear Fellows

I figured out my first question answer.

that
Code:

: mesh_(mesh), selectedCells_(selectedCells)
is for initializing fields inside constructors. its equal to

Code:

mesh_=mesh;
seectedCells_=selectedCells;

However, my second question still holds,

Why fvMesh and labelList classes are defined as References:

Code:

    const fvMesh& mesh_;
    const labelList& selectedCells_;


Zeppo August 27, 2016 13:48

Quote:

Originally Posted by babakflame (Post 615356)
that
Code:

: mesh_(mesh), selectedCells_(selectedCells)
is for initializing fields inside constructors. its equal to

Code:

mesh_=mesh;
seectedCells_=selectedCells;


No, they are not the same. Whenever the workflow of code execution reach constructor's body (an openning curly bracket {) all class member variables have allready been initialised (constructors have been called) - either explicitly with initialisation list (follows colon :) or implicitly with a default constructor. In the constructor's body (between { and }) you can only call an assignment operator to assign a new value to any allready existing class member variable.
Quote:

Originally Posted by babakflame (Post 615356)
However, my second question still holds,
Why fvMesh and labelList classes are defined as References:

Code:

    const fvMesh& mesh_;
    const labelList& selectedCells_;


Because when you construct an abject of this class you want these member variables to point to the existing objects not to create new ones, right. If they were not references but values then the constructor would have to create new copies.

babakflame August 27, 2016 14:14

Thanks Sergei.

Just for clarifying:

HTML Code:

Because when you construct an abject of this class you want these member  variables to point to the existing objects not to create new ones,  right. If they were not references but values then the constructor would  have to create new copies.
Are u considering "mesh_ and selectedCells_" in the following snippet as variables or objects?
Code:

const fvMesh& mesh_;
const labelList& selectedCells_;


Zeppo August 27, 2016 18:02

I believe mesh_ and selectedCells_ are called "member variables" and they are part of an object of iNew class. The word "variable" might imply that it can vary or be modified, but no, variable can be constant as well. And to confuse you even more, member variables are objects at the same time ;)


All times are GMT -4. The time now is 09:53.