Double colon in C++
With the help of the sophisticated programming language C++, developers have access to a variety of tools. A common element of C++ is the scope resolution operator, also referred to as the double colon (::). The double colon operators, despite being crucial to the language, can be confusing for many developers, especially those who are unfamiliar with it.
The double colon (::) operator in C++ will be discussed in this lesson, along with its numerous applications.
What is the C++ equivalent:: of its purpose?
The scope resolution operator/double colon (::) in C++ is used to get variable names at different scope levels. The scope of variables and functions in programming is used for the environment in which they can be accessed.
Uses of: in C++ in Various Situations
The scope resolution operator (::) has several uses in C++, based on the context. This operator is useful in the following situations:
- Use a variable or function from a different namespace.
- To minimize naming conflicts, C++ uses namespaces to organize related functions, classes, and variables. The name of a variable or function defined within a namespace is only accessible within that namespace.
1)Accessing a function in different namespace:
For accessing a variable or function defined by a separate namespace, make use of the scope resolution operator (::) together via the namespace name along with the variable or function name in C++. This enables us to access the variable or function inside a namespace other than the current one. This enables you to access the variable/function within a namespace apart from the one it was generated.
See the below code as an example to understand the previous case better:
Code:
#include <iostream> using namespace std; namespace math { const double PI = 3.14159; double square(double s) { return s * s; } } int main() { cout <<" Value of PI is " << math::PI << endl; cout << "Square of 7 is " << math::square(7) << endl; return 0; }
Output:
![Double colon in C++](https://static.tutorialandexample.com/cpp/double-colon-in-cpp1.png)
The above code makes use of the math namespace for accessing the "PI" constant and "square" function through the scope resolution operator "::". It then outputs the values to the console.
2)Program Access to Global Variable
When two variables with the same name appear in the program, the local variable can cover up the global one. In such cases, the scope resolution operator (::) can be used to access the global variable. This operator helps us to make references to the global variable explicitly based on its scope.
For example, in the following code, we have used the scope resolution operator(::) to refer to the global variable 'a' from within a function that has a local variable having a similar name:
Code:
#include <iostream> using namespace std; int p = 23; int main() { int p = 57; cout << "Value of global variable p is " << ::p << endl; cout << "Value of local variable p is " << p << endl; // Corrected variable name return 0; }
Output:
![Double colon in C++](https://static.tutorialandexample.com/cpp/double-colon-in-cpp2.png)
The variable p that exists on the outside of the main function is the global variable that is used in the example above. In contrast, the variable p existing inside the main function is a local variable having the same name as the global variable. The scope resolution operator was used in the cout statement to output the global variable p.
3)Create a Function Outside of the Class
Outside of the class definition, you may define a class function in C++ through the use of the scope resolution operator (::) to indicate which class the function belongs to. This is required since the function has been defined outside of the scope of the class.
Here's some code that explains how you can use the scope resolution operator:
Code:
#include <iostream> using namespace std; class Class { public: void Function(); }; void Class::Function() { cout << "Function() Called !"; } int main() { Class classobject; classobject.Function(); return 0; }
Output:
![Double colon in C++](https://static.tutorialandexample.com/cpp/double-colon-in-cpp3.png)
The scope resolution operator:: is used in the following code to denote that the member function Function() belongs to the Class class.
4)Static Class Members
When a class has a local variable with the same name as a static member, the static members will be found using the scope resolution operator (::). Now that the local variable and static member are distinct, the program can do so. This helps the program to tell the difference between a local variable and a static member. Here's some code that demonstrates how to utilize the scope resolution operator in this situation:
Code:
#include <iostream> using namespace std; class myClass { public: static int myStatVar; }; int myClass::myStatVar = 5; int main() { // Removed the local variable with the same name cout << "Class variable myStatVar: " << myClass::myStatVar << endl; // Added a new value assignment to the static class variable myClass::myStatVar = 15; cout << "Updated Class variable myStatVar: " << myClass::myStatVar << endl; return 0; }
Output:
![Double colon in C++](https://static.tutorialandexample.com/cpp/double-colon-in-cpp4.png)
Explanation:
- To deal with input and output functions, we include the appropriate input/output library (#include iostream>).
- We declare myClass, which contains a public static integer variable called myStatVar.
- Within myClass, we use int myClass::myStatVar = 5; to set the static variable myStatVar to a value of 5. This signifies that all instances of myClass will share this variable.
- We use cout to print the value of myClass::myStatVar in the main method directly. This displays the static variable's initial value, which is 5.
- The static variable myClass::myStatVar is then set to 15 with myClass::myStatVar = 15;
- Finally, we publish the static variable's updated value, 15, indicating that changes to a static variable affect all instances of the class.
5)With Multiple Inheritance
If a C++ class comes from multiple parent classes, the scope resolution operator (::) also serves to identify which version of a member to utilize. We can differentiate between different versions of similar members through the use of the scope resolution operator with the parent class name as well as the member's name.
Code:
#include <iostream> using namespace std; // Define the first parent class class Parent1 { public: void show() { cout << "This is class Parent1's show function." << endl; } }; // Define the second parent class class Parent2 { public: void display() { cout << "This is class Parent2's display function." << endl; } }; // Define a child class that inherits from both Parent1 and Parent2 class Child : public Parent1, public Parent2 { private: int value1; int value2; public: Child(int v1, int v2) { value1 = v1; value2 = v2; } void printValues() { Parent1::show(); // Call Parent1's show function Parent2::display(); // Call Parent2's display function cout << "Value 1: " << value1 << endl; cout << "Value 2: " << value2 << endl; } }; int main() { Child obj(10, 20); // Create an object of Child with values 10 and 20 obj.printValues(); // Call the printValues() function of the object return 0; }
Output:
![Double colon in C++](https://static.tutorialandexample.com/cpp/double-colon-in-cpp5.png)
Explanation:
- There are two parent classes: Parent1 and Parent2. Each parent class has a unique function: show() in Parent1 and display() in Parent2.
- Then, we define a child class called Child, which inherits from both Parent1 and Parent2. That is, it can use functions from both parent classes.
- Within the Child class, we have two private variables, value1, and value2, which hold integer values. These serve as data containers for the child class.
- When we construct a Child class object in the main() function with the values 10 and 20, we set value1 to 10 and value2 to 20.
- There is also a printValues() function in the Child class. Within the scope of this function:
- It invokes the show() function of Parent1 to display a message particular to Parent1.
- It invokes the display() method of Parent2 to display a message particular to Parent2.
- The values of values 1 and 2 are then printed to the screen.
- We create an instance (object) of the Child class with the values 10 and 20 in the main() function and use its printValues() function to see all the messages and values printed out.
This code demonstrates how a child class can inherit and use functions from multiple parent classes, along with managing its data, making it a powerful feature in object-oriented programming.
Conclusion:
The scope resolution operator (::) is a crucial component in C++ that allows you to make use of variable names across various scope levels. The operator can be used to access variables or functions in a separate namespace, access a global variable, define a function inside the class, access static members of a class, and utilize multiple inheritances. Although it can be confusing for new developers, using the scope resolution operator (::) is fundamental for good programming practice.