8

I've been wondering what does the following mean (code snippet taken from cppreference pimpl)

class widget::impl {
      ^^^^^^^^^^^^

   ...
};

What does a_class::another_class mean? Is that a namespace? Or is that an inner class declared out-of-the-main-class?

BlameTheBits
  • 850
  • 1
  • 6
  • 22
Dean
  • 6,610
  • 6
  • 40
  • 90
  • 1
    Possible duplicate of [What is the meaning of prepended double colon "::"?](http://stackoverflow.com/questions/4269034/what-is-the-meaning-of-prepended-double-colon) – Badda May 09 '17 at 13:56
  • 3
    @Badda: "prepended" means "before", not "between" or "after". (IOW, not a duplicate. The C++ grammar is very much order-sensitive.) – MSalters May 09 '17 at 14:13

4 Answers4

5

Or is that an inner class declared out-of-the-main-class?

Bingo. To be super clear, iit's actually an inner class defined outside the enclosing class.

It's a handy trick if you want a class with member-like access to your class as an implementation detail, but do not want to publish that nested class's definition to the clients of your class.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
5

The :: operator is the scope resolution operator. It qualifies the scope of an expression. In your case, it qualifies the expression class impl with the scope widget, meaning the class impl that belongs to widget. Consider the following example which defines two impl classes at different scopes :

// global impl
class impl;

class widget
{
    // widget's impl
    class impl;
};

class widget::impl
{
    // Define widget's impl
};

class impl
{
    // Define global impl
};

The scope resolution operator allows you to clearly declare which class you are defining.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
1

It is an inner class.

 class Widget
  {
    // ...

  private:
    class impl;           

  };

  // Then, typically in a separate implementation file:
  class Widget::impl
  {
  public:
    // ...
    T1 t1_;
    T2 t2_;
  };
Ðаn
  • 10,934
  • 11
  • 59
  • 95
Geek
  • 273
  • 5
  • 19
0

Could be the declaration of forward declaration of inner class e.g.:

class A{
 class B;

};

class A::B {

};
Valentin H
  • 7,240
  • 12
  • 61
  • 111