im struggling with syntax here: hopefully this question is v simple, im just miising the point.
specifically, if i nest a class within another class, so for instance
class a
{
a //the constructor
{
b an_instance_of_b // an instance of class b
}
};
class b
{
public:
foo()
{
cout << "foo";
}
};
When i try to access this method within B by doing this:
a an_instance_of_a; //declare an instance of a
an_instance_of_a.an_instance_of_b.foo()
^^ this doesnt seem to work. this is simplified (so might be a typo here somewhere). but i know the classes are being setup fine, its just that i cant access the methods inside them if they are nested. what may i be doing wrong?
many thanks.
Your
an_instance_of_bis not a member of a, but a local variable in the constructor of a (and the constructor declaration is missing the parenthesis).What will happen here is that when you create an instance of a, it creates and immediately destroys an instance of b, then it leaves the constructor for a and the a instance is created.