Search code examples
c++classscope-resolution

what does the scope resolution operator used in a class name mean


I came across this code.

class SomeClass::OtherClass : public BaseClass
{
  // stuff in here
}

SomeClass is a class, so maybe OtherClass is a class that exists inside the scope of SomeClass? I've just never seen it done this way.

So, is that what it means?


Solution

  • maybe OtherClass is a class that exists inside the scope of SomeClass?

    Give yourself a checkmark. That is what it means.

    This is used to subsequently define OtherClass after it was declared inside SomeClass:

    class SomeClass {
        class OtherClass;
        OtherClass* GetOtherClassInstance() { ...}
    };
    class SomeClass::OtherClass {
    } 
    

    One might do this if the inner class only makes sense in the context of the exterior class.

    class Vector {
      class Iterator;
      Iterator* GetStart();
    };
    class Vector::Iterator {
       details.
    }
    

    As mentioned elsewhere, the pimpl idiom is an excellent use of inner classes with deferred definition.