"Nobody You Know" <ap*******@hushmail.com> wrote...
I need a member variable in class A
"Variable in class" certainly hints at a static data member. Is
that what you're saying here? Or do you mean "any variable
declared in the class A definition", i.e. it could be non-static?
known within an instance of class
B, even though B does not instantiate A.
So,
class A {
SomeOtherType membervariable; // non-static for now
...
};
class B {
RetValType somememberfunction() { // non-static member func
???
You have to define the term "known"
and where the A comes from (see below)
???
}
};
My solution was to make the member variable private static, and create
a public static accessor function that returns the variable's value. I
reference this function in class B as ClassA::GetValueOfVariable().
Although this works, I wonder if there is a better way.
Well, whether that member is static or not is unspecified in your
statement. If it's to be static, your solution is fine. If it is
to be non-static (but per instance of A), your solution is simply
wrong, don't you think?
An instance of B can "know" an instance of A, if the A is passed to
B's member function as an argument, even if B does not instantiate
the A itself. That may change the solution, no?
Victor