Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

double colon after class name (declaration) - what does that mean?

Tags:

c++

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?

like image 862
Dean Avatar asked May 09 '17 13:05

Dean


2 Answers

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.

like image 76
Angew is no longer proud of SO Avatar answered Sep 28 '22 09:09

Angew is no longer proud of SO


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.

like image 21
François Andrieux Avatar answered Sep 28 '22 09:09

François Andrieux