CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Running, Solving & CFD (
-   -   Access private data member (

lr103476 December 17, 2008 08:12

Hi everyone, Just a more '
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

denyo December 17, 2008 10:08

Hi Frank, private data are
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,

olesen December 18, 2008 04:02

Hi Frank, Having seen your
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.

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