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

call a tmp formal value without a parentheses?

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree6Likes
  • 1 Post By sharonyue
  • 2 Post By floquation
  • 3 Post By floquation

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   December 12, 2014, 06:46
Default call a tmp formal value without a parentheses?
  #1
Senior Member
 
Dongyue Li
Join Date: Jun 2012
Location: Beijing, China
Posts: 794
Rep Power: 15
sharonyue is on a distinguished road
Hi guys,

please see this code:
Code:
tmp<fvScalarMatrix> pEqnComp1;

.......................

fluid.dgdt() =
            (
                alpha1*(pEqnComp2 & p)
              - alpha2*(pEqnComp1 & p)
            );
complete codes here:
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.
fqi0309 likes this.
sharonyue is offline   Reply With Quote

Old   December 12, 2014, 07:31
Default
  #2
Senior Member
 
floquation's Avatar
 
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 18
floquation will become famous soon enough
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 '()'-operator will return the object stored in the tmp instance. I.e., it returns a "fvScalarMatrix" in your example.

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()();
               }
To understand the C++ code, take a look at:
http://en.wikibooks.org/wiki/C++_Pro..._call_operator
http://en.cppreference.com/w/cpp/language/cast_operator
sharonyue and fqi0309 like this.
floquation is offline   Reply With Quote

Old   December 13, 2014, 04:53
Default
  #3
Senior Member
 
Dongyue Li
Join Date: Jun 2012
Location: Beijing, China
Posts: 794
Rep Power: 15
sharonyue is on a distinguished road
Quote:
Originally Posted by floquation View Post
The '()'-operator will return the object stored in the tmp instance. I.e., it returns a "fvScalarMatrix" in your example.

The third, the conversion operator, is an operator without any character. (In your example: "pEqnComp1", without brackets '()'.)
Hi bro,

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!
sharonyue is offline   Reply With Quote

Old   December 13, 2014, 13:10
Default
  #4
Senior Member
 
floquation's Avatar
 
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 18
floquation will become famous soon enough
I'm not 100% sure what you are asking. But I think you are asking the following:

Quote:
Does 'pEqnComp1' and 'pEqnComp1()' do the same?
And of what type are they?
Ultimately, the pEqnComp1 object is a tmp<fvScalarMatrix>.
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.
sharonyue, fqi0309 and ugurtan666 like this.
floquation is offline   Reply With Quote

Old   May 23, 2020, 17:48
Default
  #5
New Member
 
Fenglei
Join Date: Mar 2019
Location: Luxembourg
Posts: 1
Rep Power: 0
fqi0309 is on a distinguished road
Your explanation is very helpful. I was confused by the same question for some time.
__________________
Fenglei
fqi0309 is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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


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 13:51
writing link between two CGNS files t.teschner Main CFD Forum 1 February 4, 2014 10:26
[OpenFOAM] Annoying issue of automatic "Rescale to Data Range " with paraFoam/paraview 3.12 keepfit ParaView 60 September 18, 2013 03:23
2D CFD code using SIMPLE algorithm bfan Main CFD Forum 3 June 22, 2002 22:01
Who's ok for an Open Source CFD project ? Viet Main CFD Forum 16 July 26, 1999 15:57


All times are GMT -4. The time now is 21:52.