简体   繁体   中英

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?

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.

It means that OtherClass is an inner class of SomeClass. It had better already have been declared there. Works nice for the pimpl idiom:

struct my_object {
  ...

private:
  struct my_impl;
  my_impl * pimpl;
};

// in a cpp file...
struct my_object::my_impl {
  ...implementation details of my_object
};

I think SomeClass is namespace in which OtherClass resides

class BaseClass {};
namespace SomeClass
{
    class OtherClass;
};

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

some class is a name of base class and other class is a derived class which link the inside of derived class and after the colon derived class link the base class. In your question the answer is two base class and one derived class are mention.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM