9

The title already says a lot,

but basically what i want to do is the following(Example):

I have a class called A, and another class inside a called B, like so:

class A
{
   int a;

   class B
   {
      void test()
      {
         a = 20;
      }
   };
};

As you can see my goal is for class B to have access to class A, as it is a nested class. Not this wont work, because B doesn't have access to A, but how can it get access?

Thank You

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
Miguel P
  • 1,262
  • 6
  • 23
  • 48

5 Answers5

13

Despite that you declared class B inside of A, classes A and B are still completely independent. The only difference is that now to refer to B, one must do A::B.

For B to access A's stuff, you should use composition or inheritance. For composition, give B a reference to an object of A, like so:

class B {
public:
  B(const A& aObj) : aRef(aObj) {
    cout << aRef.a << endl;
  }
private:
  const A& aRef;
};

For inheritance, something like this:

class B: public A { // or private, depending on your desires
  B() {
    cout << a << endl;
  }
}
Philip
  • 1,532
  • 12
  • 23
  • Just to clarify, B would no longer be nested in the example above. Otherwise an object of type B would contain an object of type A which would contain an object of type B... – mythagel Oct 11 '12 at 22:03
  • Your code for class B(the first block) doesn't give any sense for me... The constructor's brackets. – Miguel P Oct 11 '12 at 22:11
  • Thanks, but there's only one problem now, whenever i try to access a member of a, it just gives me an error: cannot convert const typea to type a(simple version) – Miguel P Oct 11 '12 at 22:18
  • First, can you make A's methods const? If so, do it. Otherwise remove const-ness from the reference declaration. I added it because it's good practice, but it may be too restrictive for you. – Philip Oct 11 '12 at 22:20
  • I tried the first approach (composition) and ran into a serious problem - what happens if an outer (A) object is copied or moved? Then the inner (B) object has a dangling reference to the old object. I fixed the problem by avoiding this construct entirely - I pass an explicit reference to the outer object to every inner object method that needs it. – Brent Baccala Jan 20 '16 at 03:45
2

The inner class is not related to the outer class in C++ as it is in Java. For an instance of A::B to access a member of an A object, it needs to have an instance of A somewhere, just as if B were not a nested class. Instances of A::B do not have any implicit instance of A; you can have many instances of A::B without any instances of A existing at all.

Pass an instance of A to test, and then use it to access the a member:

void test(A& a_instance)
{
  a_instance.a = 20;
}
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
1

Classes are types, types don't have data. Instances have data, but an instance of A does not (in your example) contain an instance of B, and the instances of B don't have any knowledge of any instance of A.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
0

Choices

  • have B be a child of A instead of contained by A
  • have B's constructor take a ref to the A instance which created it (preferred)

Now, if the variable a is private this still won't help. You will either need an accessor a or a friend relation.

Sean Perry
  • 3,776
  • 1
  • 19
  • 31
-1

C++ nested classes are not like java nested classes, they do not belong to an instance of A but are static. So a doesn't exist at that point