Parent Function?

Hi, I'm new to C++ and I'm finding this site extremely helpful!
I've now figured out how to reference functions from inherited classes, but now I have a similar question.
If I have a class that is a member of another class, is there a way to reference the one that has the members from the member?
I'm not sure if that's called a parent or not, but do you see what I mean?

example:
class A
{

}
class B
{
A member;
int bInfo;
B()
{
bInfo=3;
}
}
main()
{
A classA;
//how do I get sumeNumber set to 3 from class B through class A?
// is it possible? I imagine something like this:
// int someNumber=classA.parentMeaningBsomehow????.bInfo;
}

Does anyone know if this is possible and if so how?

Thanks,

Sean =)
No, they aren't child/parent because class B is not inheriting from class A. If you want to access the class A inside of B the way you have it currently, use ClassB.member./*stuff*/. Also, your question seems to imply that A is inheriting from B, which is also not correct. Read here for more info:

http://www.cplusplus.com/doc/tutorial/inheritance/#inheritance
Right, so what if I want to access Class B from inside a function of class A without directly referring to class B?
Is there something like an inverse function to call the class it is a member of? Like an arrow pointing the other way--LOL--classA<-classB???
Last edited on
No. You seem to have a fundamental misunderstanding of how classes/structs work.

You can't access [nonstatic] B members without a B object... because B members don't exist outside of B objects, therefore there's nothing to access.

Since creating an A object doesn't create a B object, you have no B object in your A object, and therefore there is no way to access any B members.
Ok, you are right, so how about this:
example:
class A
{
int sumNum;
void printBinfo()
{
sumNum=?????;
//how do I get sumeNumber set to 1 from class B (instance classB1) and set to 2 from class B (instance classB2) through class A?
// is it possible? I imagine something like this:
// int someNumber=classA.parentMeaningBsomehow????.bInfo;
cout <<sumNum;
}
}
class B
{
A member;
int bInfo;
B()
{
bInfo=3;
}
}
main()
{
B classB1;
B classB2;
classB1.bInfo=1;
classB2.bInfo=2;

classB1.classA.printBinfo();
classB2.classA.printBinfo();
classB2.classA.printBinfo();
classB1.classA.printBinfo();
}
In the future, use [code]code tags[/code].

As for your problem, pass a parameter to 'printBinfo'. Either classB1.bInfo via type 'int' or classB via type 'const B&':

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class A
{
  int sumNum;
  void printBinfo(int bval)
  {
    sumNum = bval;
  }
};

int main()
{
  B myB;
  A myA;

  myA.printBinfo( myB.bInfo );
}
Last edited on
Right, but suppose I have much more complex code than classB1 and classB2, and that I don't know which instance of class B I'm in. The whole point is to find out which class B instance the class A was a member of or to access it for any other purpose. I'm just looking for a backwards to the . or -> operators. Is there such a thing?
So give a pointer to the B object instead of an individual value.

And no there is no backwards . or ->.
Last edited on
Topic archived. No new replies allowed.