Chris Morris
Chris Morris

Reputation: 4444

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?

Upvotes: 5

Views: 4557

Answers (4)

adil
adil

Reputation: 1

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.

Upvotes: 0

Edward Strange
Edward Strange

Reputation: 40897

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
};

Upvotes: 3

Forever Learner
Forever Learner

Reputation: 1483

I think SomeClass is namespace in which OtherClass resides

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

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

Upvotes: 0

Robᵩ
Robᵩ

Reputation: 168806

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.

Upvotes: 11

Related Questions