views:

49

answers:

2

Hi

lets say i Have:

class A {

public:
    class B {

    };

};

is there any difference between that public nested class an just a regular B class which is defined in its own cpp file, except for the face that A::B must be used in the first option?

Thanks!

+7  A: 

There is essentially no difference, except that A::B is a member of A, and so has all the access rights to private members of A that any other member would have.

Anthony Williams
Hi.. i've created a public nested B class and i can't see how it accesses the private variables of A.. i guess i don't know the syntax..?
rob
@rob: It needs an object of type A. It can then access it's members.
Martin York
There is no special syntax. `B` is a member of `A`, so it can access an `A` instance's private variables and `A`'s private static functions.
André Caron
@Anthony: I don't know if this was tightened or loosened in the last update of the standard. Can you quote the standard and the version because the wording of this did change.
Martin York
Found it: http://stackoverflow.com/questions/3537969/nested-classes-access-to-protected-member-of-enclosing-class-from-a-nested-prote/3537998#3537998
Martin York
A: 

There isn't any difference other than the scoping rules for "B". Clients that use "B" must qualify its scope with "A::". Nesting the "B" can sometimes be problematic when you want to forward reference it, since C++ compilers typically do not allow you to forward reference a class within a class (it does allow you to forward reference a class within a namespace though).

Astro Weasel