Re: Could an instance of a small class as a member of a bigger class access the bigger class's static variable?
On 26 Mrz., 14:38, Xiaozhong <sullivanz....@gmail.com> wrote:
I am making a simulation program in which I constructed 2 classes. A
small one which describe a single atom (cls_atom), and a large one,
which is the container of the simulation containing lots of atoms
(cls_box). Therefore cls_atom will create thousands of instances as
member of cls_box.
Now there are lots of member variables of the cls_box needed to be
shared by all instances of cls_atom. For example, temperature T,
pressure P and total volume of the box, V, which is the global
parameter for the sandbox and should be accessed by all atoms.
Is there a way to authorize such access, so that T, P, and V can be
accessed (read only or read/write) by member cls_atom instances. It
will just be like T, P and V are "global" inside the cls_box class.
Thanks a lot for any advice.
Just to be sure: When you speak of "access" to you mean
access control as specified by the C++ language? I ask,
because I wonder why these fundamental physical parameters
are not already public available from you sandbox. Aren't
these the characteristics of this sandbox anyway?
In this spirit I expected that you have something like
the following in your mind
class cls_atom {
...
};
class cls_box {
private:
std::vector<cls_atom> atoms;
double temperature_;
double pressure_;
double volume_;
...
public:
double temperature() const;
double pressure() const;
double volume() const;
...
};
which would be the most natural approach, given your
description. Even, if not, the private members of
cls_box, that describe P, T, and V will be available
within the member functions of cls_box anyway.
I'm unsure in which respect the atoms of the box
themselves would need to change the environment,
because their collective results in the effective P, T,
and V values or vice verse: These parameters are
used to describe the collective. Could you explain
where the access problem occurs in the above
scenario?
[The problem is not *how* to provide access. There
are different solutions for this, but these depend
on the actual problem. The lack of understanding of
latter is the reason for lack of advice here].
Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]