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

Access private data member

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 1 Post By denyo
  • 1 Post By olesen

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   December 17, 2008, 08:12
Default Hi everyone, Just a more '
  #1
Senior Member
 
Frank Bos
Join Date: Mar 2009
Location: The Netherlands
Posts: 340
Rep Power: 18
lr103476 is on a distinguished road
Hi everyone,

Just a more 'general' C++ programming question. What is the best way to access private data member used in i.e. dynamicBodyFvMesh from your application, i.e. icoDyMFoam.C ?

Regards, Frank
__________________
Frank Bos
lr103476 is offline   Reply With Quote

Old   December 17, 2008, 10:08
Default Hi Frank, private data are
  #2
New Member
 
Daniel Conrad
Join Date: Mar 2009
Posts: 1
Rep Power: 0
denyo is on a distinguished road
Hi Frank,

private data are accessed via a public function, which returns the value of a private variable (by convention indicated with underscore: privateVariable_) or a reference (&privateVariable_) to it.
Look through any .H file to look up you possibilities to access private data using function declared as public memberfunctions.

dynamicBodyFvMesh is a meshtype and needed for the runTimeSelectionTable. Nevertheless it is derived from fvMesh. Goto src/finiteVolume/fvMesh.H to see your options.

best regards,
Daniel
Pascal_doran likes this.
denyo is offline   Reply With Quote

Old   December 18, 2008, 04:02
Default Hi Frank, Having seen your
  #3
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,715
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Hi Frank,

Having seen your talk in Milano, I suspect that you're probably okay with C++ per se, but for your current question:

if you need read-only access to a private member, just add a public const member to the header, eg.

const foo& bar() const
{
return bar_;
}

and let the OpenCFD guys know that you need read access to the data for whatever reason so they can add it to the header for everyone.

If you need write-access to the data member, you should either question if this is the correct approach at all, or if you actually need a derived class. If the latter is the case, it would be reasonable to provide a protected write-accessor so that your derived class can monkey with the data.

One important note, however: be careful about mixing const/non-const and private/public declarations for the same member signature. If a non-const member is declared private and a const member of the same name and signature is declared public, the compiler cannot tell which one is meant when compiling your application. A simplified explanation: the first compiler pass just takes the best signature, but doesn't bother to work out if const access would have sufficed and you'll get lots of compiler complaints.
Kummi likes this.
olesen is offline   Reply With Quote

Old   May 5, 2018, 22:44
Default
  #4
Member
 
chengan.wang
Join Date: Jan 2016
Location: china
Posts: 47
Rep Power: 10
wangchengan2003 is on a distinguished road
Send a message via Skype™ to wangchengan2003
Hello olesen,

I want to use incident radiation 'G' in my solver, which is also a private data in 'fvDOM.H'.

In c++ code, it writes

Quote:
/*---------------------------------------------------------------------------*\
Class fvDOM Declaration
\*---------------------------------------------------------------------------*/

class fvDOM
:
public radiationModel
{
// Private data


//- Incident radiation [W/m2]
volScalarField G_;
Quote:
//- Const access to incident radiation field
inline const volScalarField& G() const;
In my solver, I use
Quote:
radiation::fvDOM fvDOM(T);

const volScalarField& G = fvDOM.G();
in 'createFields.H'. Then I use G to calculate T in 'TEqn.H', such as
Quote:
T=pow(G/(4*sigma),0.25);
Unfortunately, I found that T is always zero. That means I did not read the G value exactly.

Now I have no idea. If you could give me some help?

Best regards,

Chengan
wangchengan2003 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


Similar Threads
Thread Thread Starter Forum Replies Last Post
question on data access in parallel computation wangrensong FLUENT 0 January 7, 2009 04:24
UDF Data Access Macro Woo Meng Wai FLUENT 0 November 6, 2007 21:23
UDF's to access Postprocess data ivor FLUENT 1 December 15, 2003 11:07
access to FIDAP-internal data from within a UDF II Ingo Meisel FLUENT 1 April 23, 2001 12:47
access to FIDAP-internal data from within a UDF Ingo Meisel FLUENT 0 April 17, 2001 12:58


All times are GMT -4. The time now is 04:36.