|
[Sponsors] |
call a tmp formal value without a parentheses? |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
December 12, 2014, 07:46 |
call a tmp formal value without a parentheses?
|
#1 |
Senior Member
Dongyue Li
Join Date: Jun 2012
Location: Beijing, China
Posts: 849
Rep Power: 18 |
Hi guys,
please see this code: Code:
tmp<fvScalarMatrix> pEqnComp1; ....................... fluid.dgdt() = ( alpha1*(pEqnComp2 & p) - alpha2*(pEqnComp1 & p) ); https://github.com/OpenFOAM/OpenFOAM...lerFoam/pEqn.H we can see pEqnComp1 is a tmp format, shouldnt it be called as: pEqnComp1()? I tried with both, all works...why? any body knows? Thanks. |
|
December 12, 2014, 08:31 |
|
#2 |
Senior Member
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 21 |
The answer is "operator overloading". More specifically "overloading the conversion operator".
The 'tmp' class is OpenFOAM's version of a "smart pointer" (a pointer which cleans up after itself). The 'tmp' class overloads the following operators (among others): Code:
//- Dereference operator inline T& operator()(); //- Const dereference operator inline const T& operator()() const; //- Const cast to the underlying type reference inline operator const T&() const; The third, the conversion operator, is an operator without any character. (In your example: "pEqnComp1", without brackets '()'.) Looking at the implementation of these operator overloads, it can be seen that the conversion operator will call the '()'-operator: Code:
template<class T> inline Foam::tmp<T>::operator const T&() const { return operator()(); } http://en.wikibooks.org/wiki/C++_Pro..._call_operator http://en.cppreference.com/w/cpp/language/cast_operator |
|
December 13, 2014, 05:53 |
|
#3 | |
Senior Member
Dongyue Li
Join Date: Jun 2012
Location: Beijing, China
Posts: 849
Rep Power: 18 |
Quote:
thanks for the link, actually Im new to C++, im confused that, is there any difference between pEqn()"it returns a "fvScalarMatrix" in your example" and pEqn"(In your example: "pEqnComp1", without brackets '()'" Because I thought pEqn is fvScalarMatrix, also the same with pEqn(). they are all fvScalarMatrix? .... Thanks! |
||
December 13, 2014, 14:10 |
|
#4 | |
Senior Member
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 21 |
I'm not 100% sure what you are asking. But I think you are asking the following:
Quote:
Thanks to how OpenFOAM implemented 'operator overloading' in their tmp-class, typing 'pEqnComp1()' or 'pEqnComp1' (as a statement) will insert the fvScalarMatrix-object hidden within the tmp-object at the position you typed the statement 'pEqnComp1()' or 'pEqnComp1'. So, both statements ('pEqnComp1()' and 'pEqnComp1') will give you a fvScalarMatrix object. Carefully distinguish between the object pEqnComp1 and the statement/command/function-call pEqnComp1. The latter statement ('pEqnComp1' or 'pEqnComp1()') calls a function of the pEqnComp1 object. |
||
May 23, 2020, 18:48 |
|
#5 |
New Member
Fenglei
Join Date: Mar 2019
Location: Luxembourg
Posts: 1
Rep Power: 0 |
Your explanation is very helpful. I was confused by the same question for some time.
__________________
Fenglei |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Creating a new field from terms of the turbulence model | HaZe | OpenFOAM Programming & Development | 15 | November 24, 2014 14:51 |
writing link between two CGNS files | t.teschner | Main CFD Forum | 1 | February 4, 2014 11:26 |
[OpenFOAM] Annoying issue of automatic "Rescale to Data Range " with paraFoam/paraview 3.12 | keepfit | ParaView | 60 | September 18, 2013 04:23 |
2D CFD code using SIMPLE algorithm | bfan | Main CFD Forum | 3 | June 22, 2002 23:01 |
Who's ok for an Open Source CFD project ? | Viet | Main CFD Forum | 16 | July 26, 1999 16:57 |