Search code examples
c++scope-resolution-operator

What does the "::" mean in C++?


What does this symbol mean?

AirlineTicket::AirlineTicket()

Solution

  • :: is the scope resolution operator - used to qualify names. In this case it is used to separate the class AirlineTicket from the constructor AirlineTicket(), forming the qualified name AirlineTicket::AirlineTicket()

    You use this whenever you need to be explicit with regards to what you're referring to. Some samples:

    namespace foo {
      class bar;
    }
    class bar;
    using namespace foo;
    

    Now you have to use the scope resolution operator to refer to a specific bar.

    ::foo::bar is a fully qualified name.

    ::bar is another fully qualified name. (:: first means "global namespace")

    struct Base {
        void foo();
    };
    struct Derived : Base {
        void foo();
        void bar() {
           Derived::foo();
           Base::foo();
        }
    };
    

    This uses scope resolution to select specific versions of foo.