10

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.

user2444217
  • 581
  • 2
  • 7
  • 16
  • 1
    http://en.wikipedia.org/wiki/Scope_resolution_operator – Son-Huy Pham Jun 28 '13 at 20:35
  • 3
    `::` is the scope resolution operator - it means, that `SomeClass` method is in `Some_Class` – Shf Jun 28 '13 at 20:36
  • 3
    You need a good introductory book about C++. Take a look here http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list. – Johan Råde Jun 28 '13 at 20:40
  • @user2444217: Why is the first part different from the second one? `Some_Class` and `SomeClass`. Is it a typo? If it is, then what you posted is a rather meaningless sequence of tokens. It could be a constructor declaration, if it included parameter declarations. Or it could be a nested class definition, if it included a `class/struct` keyword. But as it is now, it is completely meaningless. Clarify your question. – AnT stands with Russia Jun 28 '13 at 21:53

3 Answers3

13

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
6

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;
Captain Skyhawk
  • 3,499
  • 2
  • 25
  • 39
3

You are confused by the scope resolution operator (thanks @Huytard for the link)

:: is the scope resolution operator - it means, that SomeClass method is in Some_Class, given your example -Some_Class::SomeClass

Shf
  • 3,463
  • 2
  • 26
  • 42