Unimatrix_001
asked on
Inner class accessing outer class private variables.
Hi.
class ClassA{
public:
class ClassB{
}
private:
int aVar;
}
How can I allow the methods of classB to use the variable in class A in a tidy fashion?
Thanks,
Uni
class ClassA{
public:
class ClassB{
}
private:
int aVar;
}
How can I allow the methods of classB to use the variable in class A in a tidy fashion?
Thanks,
Uni
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Unimatrix_001,
It only makes sense to declare a nested (or inner) class if you instantiate the inner class.
class ClassA{
public:
class ClassB{
}
private:
classB *B;
int aVar;
}
Now your options are wide open. You can pass aVar to the classB instantiator which saves it in the inner class, or pass it to a method in the inner class.
ClassA *A;
A = new ClassA;
A->B = new ClassB;
Good Luck,
Kent
It only makes sense to declare a nested (or inner) class if you instantiate the inner class.
class ClassA{
public:
class ClassB{
}
private:
classB *B;
int aVar;
}
Now your options are wide open. You can pass aVar to the classB instantiator which saves it in the inner class, or pass it to a method in the inner class.
ClassA *A;
A = new ClassA;
A->B = new ClassB;
Good Luck,
Kent
ASKER
Hi Zoppo: Thanks for that, although I can't pass an instance as what I'm wanting int classA is static...
ASKER
Kent: The reason for the nested class is to seperate certain methods in classA from other methods in classA while keeping all the methods within classA... That sounds a bit of a mouthful, but nevertheless there is logical reasoning behind it. ;)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hehe, it does seem a bit of a muddle... Still, it seem that's the only way to do it.
Thanks :)
Uni.
Thanks :)
Uni.
Just make sure, that inner class' methods are defined after outer class variables definition.
And of course inner class have to have reference(pointer) to instance of superclass.