Max
Max

Reputation: 3914

The complete name of the double colon in 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?

Upvotes: 6

Views: 1310

Answers (3)

leftaroundabout
leftaroundabout

Reputation: 120711

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.

Upvotes: 7

John Humphreys
John Humphreys

Reputation: 39284

It's called the scope resolution operator.

Upvotes: 13

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234434

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.

Upvotes: 15

Related Questions