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?
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:
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???
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;
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?