Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ classes use of double colon [duplicate]

Tags:

c++

I am learning C++. Now I don't fully understand what this does

Some_Class::Some_Class {
    etc...
}

I would do some research for myself, but I'm not sure where to begin or what's it called. Help would be appreciated.

like image 255
user2444217 Avatar asked Jun 28 '13 20:06

user2444217


People also ask

What is the use of double colon in C?

The Scope Resolution Operator in C++ Two colons (::) are used in C++ as a scope resolution operator. This operator gives you more freedom in naming your variables by letting you distinguish between variables with the same name.

What does double colon mean in code?

a notation for equality of ratios. a scope resolution operator, in computer programming languages.

What is the use of :: in Java?

The double colon (::) operator, also known as method reference operator in Java, is used to call a method by referring to it with the help of its class directly. They behave exactly as the lambda expressions.

What does double semicolon mean in CSS?

It means pseudo element selector. It means the element to the right doesn't exist in the normal DOM, but can be selected. A pseudo-element is made of two colons (::) followed by the name of the pseudo-element. Source.


2 Answers

The :: resolves either a class or namespace.

For example

namespace test1 { int i = 0; }
cout << test1::i << endl;

or

class test2 { 
     public: 
     static int i = 0; 
 };
 // after in
cout << test2::i << endl;

You can also add this:

using namespace test1;
cout << i << endl;
like image 170
Captain Skyhawk Avatar answered Oct 22 '22 14:10

Captain Skyhawk


There's no way to say what it is, since the "code" you posted is invalid and ambiguous.

  • It could be a nested class definition made in out-of-class fashion. When you define nested classes, you can immediately define the inner class inside, as in

    class Some_Class {   // <- definition of the outer class
      ...
      class SomeClass {  // <- definition of the inner class
        ...
      };
      ...
    };
    

    Or, if you prefer, you can only declare the nested class inside, and move the actual definition outside

    class Some_Class {   // <- definition of the outer class
      ...
      class SomeClass;   // <- declaration of the inner class
      ...
    }; 
    
    class Some_Class::SomeClass { // <- definition of the inner class
      ...
    };
    

    However, for that it has to begin with class/struct, which is not present in what you posted.

  • Or it could be a definition of member function SomeClass of class Some_Class.

    class Some_Class {
      ...
      void SomeClass(int i);          // <- declaration of member function
      ...
    };
    
    void Some_Class::SomeClass(int i) // <- definition of member function
    {
      ...
    }
    

    But for that it has to include return type and parameter list.

  • Or it could be a definition of a static member with {}-enclosed initializer

    class Some_Class {
      ...
      static int SomeClass;
      ...
    };
    
    int Some_Class::SomeClass { 42 };
    

    But for that it has to include static member's type.

In other words, there's no way to say what it is you posted and what your question is really about.

like image 44
AnT Avatar answered Oct 22 '22 13:10

AnT