469,655 Members | 1,852 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,655 developers. It's quick & easy.

Passing Data From One Class to Another

I need a member variable in class A known within an instance of class
B, even though B does not instantiate A.

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.
Jul 22 '05 #1
1 2656
"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
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Simon Harvey | last post: by
6 posts views Thread by glenn | last post: by
10 posts views Thread by Stan | last post: by
reply views Thread by Iain McIntosh | last post: by
9 posts views Thread by itarizin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.