Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The complete name of the double colon in C++

Tags:

c++

If I have a class:

class A{
public:
    A();
    void print();
private:
    int value;
};

A::A() {value = 0;}
void A::print() {cout << value << endl;}

What is the complete name of the :: symbol in the last 2 lines?

like image 299
Max Avatar asked Jun 07 '12 23:06

Max


People also ask

What is 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 is double colon symbol called?

The double colon ( :: ) may refer to: an analogy symbolism operator, in logic and mathematics. a notation for equality of ratios.

What is the :: in C++?

In C++, scope resolution operator is ::. It is used for following purposes. 1) To access a global variable when there is a local variable with same name: // C++ program to show that we can access a global variable. // using scope resolution operator :: when there is a local.

What do colons mean in C?

It's commonly used to pack lots of values into an integral type. In your particular case, it defining the structure of a 32-bit microcode instruction for a (possibly) hypothetical CPU (if you add up all the bit-field lengths, they sum to 32).


3 Answers

What is the complete name of the :: symbol in the last 2 lines?

It's "scope resolution operator".

Does anyone know the answer?

Yes.

Is this the weirdest question you ever been asked?

No.

like image 89
R. Martinho Fernandes Avatar answered Oct 05 '22 16:10

R. Martinho Fernandes


It's called the scope resolution operator.

like image 36
John Humphreys Avatar answered Oct 05 '22 14:10

John Humphreys


It's called scope resolution operator.


You'd like to know what you could write instead of ::? Well, there is no alternative that always works. For your example, it is possible to just define those member functions in the body of your class, that would be the inline-style of defining a class:
class A{
  int value;
 public:
  A() {
    value = 0;
  }
  void print() {
    cout << value << endl;
  }
};

That way, you obviously have no way to put the definition in a different file, so it's not possible to compile them separately.

At other times, when :: is used to resolve a namespace rather than a class, you can replace that with either reopening that namespace or pulling it into scope with using namespace.

like image 31
leftaroundabout Avatar answered Oct 05 '22 14:10

leftaroundabout