C++

What Does :: Do in C++?

C++ is a versatile programming language that offers developers a wide range of functionality. One of the most commonly used features in C++ is the scope resolution operator, also known as the double colon (::). Despite being a vital part of the language, the double colon operator can be confusing for many developers, particularly those who are new to it.

In this guide, we will look at the double colon (::) operator in C++ and how it works in different scenarios.

What Does :: Do in C++?

In C++, the scope resolution operator (::) is used to retrieve the variable’s names present at various scope levels. The scope in programming refers to the context where variables and functions can be accessed.

Uses of :: in Different Cases in C++

In C++, the scope resolution operator (::) has various uses depending on the context in which it is used. The following are some notable uses of this operator:

1: Access a Variable or Function in a Different Namespace

A namespace is used in C++ to group related functions, classes, and variables to avoid naming conflicts. When you define a variable or function within a namespace, the variable or function’s name is only visible within that namespace.

In C++, you can use the scope resolution operator (::) along with the namespace name and the variable or function name to access a variable or function defined in a different namespace. This allows you to access the variable or function from a namespace other than the one in which it was created.

To understand the above case, consider the following code as an example:

#include <iostream>

using namespace std;

namespace math {

  const double PI = 3.14159;

  double square(double x) {

    return x * x;

  }

}

int main() {

  cout << "The value of PI is: " << math::PI << endl;

  cout << "The square of 5 is: " << math::square(5) << endl;

  return 0;

}

The above code uses the math namespace to access the “PI” constant and “square” function using the scope resolution operator “::”. It then prints the values to the console.

Output

2: Access Global Variable in Program

When the same name of local and global variables is present in the program, the local variable may hide the global one. In such situations, you can use the scope resolution operator (::) for accessing the global variable. This operator allows us to explicitly refer to the global variable based on its scope.

For example, in the code given below, we have referred to the global variable ‘a‘ via the scope resolution operator(::), from inside a function where a local variable with the same name has been defined:

#include <iostream>

using namespace std;

int a = 15;

int main()

{

  int a = 35;

  cout << "The Value of global variable a is " << ::a << endl;

  cout << "The Value of local variable a is " << a << endl;

  return 0;

}

In the above code, the variable a present outside the main function is the global variable and the variable a within the main function is the local variable with the same name as the global variable. In the cout statement, we have used the scope resolution operator to print the global variable a.

Output

3: Define a Function Outside the Class

In C++, you can define a class function outside of the class definition and it can be done using the scope resolution operator (::) to specify which class the function belongs to. This is necessary because the function is defined outside the class scope.

Here is an example code that demonstrates this use of the scope resolution operator:

#include <iostream>

using namespace std;

 

class myClass {

public:

  void myfunction();

};

void myClass::myfunction() {

  cout << "myfunction() Called !";

}

int main()

{

  myClass classObj;

  classObj.myfunction();

  return 0;

}

In the above code, the member function myfunction() is defined outside the class using the scope resolution operator :: to specify that myfunction() belongs to the myClass class.

Output

4: Access Static Members of the Class

In C++, when there is a static member and a local variable with the same name is present within the class, the scope resolution operator (::) can be used to access static members of a class. This allows the program to distinguish between the local variable and the static member.

Here is an example code that relates the use of scope resolution operator for such a case:

#include <iostream>

using namespace std;

class myClass {

public:

  static int myStatVar;

};

int myClass::myStatVar = 5;

  int main() {

  int myStatVar = 10;

  cout << "Local variable myStatVar: " << myStatVar << endl;

  cout << "Class variable myStatVar: " << myClass::myStatVar << endl;

  return 0;

}

The above program first defines a class myClass with a static member variable myStatVar. It then defines a local variable with the same name inside the main function of the program. To access the class variable, the scope resolution operator (::) is used with the class name myClass. The program outputs the value of both variables to the console.

Output

5: Use with Multiple Inheritances

The scope resolution operator (::) is also used to indicate which version of a member to use when a C++ class is derived from multiple parent classes that have member variables or functions with the same name. We can distinguish between various versions of the same member by using the scope resolution operator followed by the parent class name and the member’s name.

#include <iostream>

using namespace std;

class Parent1 {

public:

  void print() {

    cout << "This is class Parent1 print function." << endl;

  }

};

class Parent2 {
public:
    void print() {
        cout << "This is class Parent2 print function." << endl;
    }
};
class myClass : public Parent1, public Parent2 {
private:
    int num;

public:
    myClass(int n) {
        num = n;
    }
    void print() {
        Parent1::print(); // call Parent1's print function
        Parent2::print(); // call Parent2's print function
        cout << "The value of num is: " << num << endl; // print out the value of num
    }
};
int main() {
    myClass obj(42); // create an object of MyClass with num initialized to 42
    obj.print(); // call the print() function of the object
    return 0;
}

The above program demonstrates the use of the scope resolution operator (::) to distinguish between the print functions of Parent1 and Parent2 when both classes are inherited by myClass. By calling the functions with the Parent class name preceding the function name, we can specify which version of the function to use.

Output

Bottom Line

The scope resolution operator (::) is a vital component in C++ used to access variable names across different scope levels. The operator has various uses, including accessing a variable or function in a different namespace, accessing a global variable, defining a function outside the class, accessing static members of a class, and using multiple inheritances. Although it can be confusing for new developers, understanding the scope resolution operator (::) is essential for good programming practice.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.