CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

MultiRegionSetFields.C

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 1 Post By Zeppo
  • 1 Post By Zeppo

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   August 23, 2016, 23:24
Default MultiRegionSetFields.C
  #1
Senior Member
 
Bobby
Join Date: Oct 2012
Location: Michigan
Posts: 454
Rep Power: 16
babakflame is on a distinguished road
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 is offline   Reply With Quote

Old   August 24, 2016, 13:17
Default
  #2
Senior Member
 
Bobby
Join Date: Oct 2012
Location: Michigan
Posts: 454
Rep Power: 16
babakflame is on a distinguished road
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_;
babakflame is offline   Reply With Quote

Old   August 27, 2016, 14:48
Default
  #3
Senior Member
 
Zeppo's Avatar
 
Sergei
Join Date: Dec 2009
Posts: 261
Rep Power: 21
Zeppo will become famous soon enough
Quote:
Originally Posted by babakflame View Post
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 View Post
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 likes this.
Zeppo is offline   Reply With Quote

Old   August 27, 2016, 15:14
Default
  #4
Senior Member
 
Bobby
Join Date: Oct 2012
Location: Michigan
Posts: 454
Rep Power: 16
babakflame is on a distinguished road
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_;

Last edited by babakflame; August 27, 2016 at 17:47.
babakflame is offline   Reply With Quote

Old   August 27, 2016, 19:02
Default
  #5
Senior Member
 
Zeppo's Avatar
 
Sergei
Join Date: Dec 2009
Posts: 261
Rep Power: 21
Zeppo will become famous soon enough
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
babakflame likes this.
Zeppo is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 08:50.