×

Scope Resolution Operator vs this Pointer in C++

In this tutorial, we will compare the Scope Resolution operation to this Pointer in C++ language.

Scope Resolution Operator

The Scope Resolution Operator in C++ programming language is usually denoted by (::). It is used in various Scenarios such as global variables, defining a function, accessing class static variables, multiple inheritances, referring to another class etc.

1. Accessing the Global and Local Variables with the same Terminology

Code:

#include<iostream> 
using namespace std;
int xyzz;
int main(){
  int x = 10; // Local x
  cout << "The value of global xyzz variable is " << ::xyzz;
  cout << "\nThe Value of local the same xyzz variables " << xyzz;  
  return 0;
}

Output:

The value of global xyzz variable is 0
The Value of local the same xyzz variables 0

2. Defining a Function Code outside of the Class

Code:

#include<iostream> 
using namespace std;
class A_b_c{
public: 
void fun();
};
void A_b_c::fun(){
   cout << "fun() called done over";
}
int main(){
   A_b_c a;
   a.fun();
   return 0;
}

Output:

fun() called done over

3. To access a Class of Static Variables

Code:

#include<iostream>
using namespace std;
class Test{
static int x;  
public:
    static int y;   
    void func(int x)  
    { 
cout << "static x value is " << Test::x;
cout << "\nlocal x value is " << x;  }
};
int Test::x = 1;
int Test::y = 2;
int main(){
    Test obj;
    int x = 3 ;
    obj.func(x);
    cout << "\nTesting for the value of ::y = " << Test::y;
return 0;
}

Output:

the static x value is 1
the local x value is 3
Testing for the value of::y = 2

4. Multiple Inheritances

Code:

#include<iostream>
using namespace std;
class A_b{
protected:
    int x;
Public:
    A_b() { x = 1576570; }
};
class B_c{
protected:
    int x;
Public:
    B_c() { x = 12220; }
};
class C_d: public A_b, public B_c{
Public:
   void fun()
   {
      cout << "A_b's x is " << A_b::x;
      cout << "\nB_c's x is " << B_c::x;
   }
};
int main(){
    C_d c;
    c.fun();
    return 0;
}

Output:

A_b's x is 1576570
B_c's x is 12220

5. In a particular Namespace Case

Code:

// Here, as we can see, there is no namespace std; hence we are
//using the scope resolution operator for the absence of namespace
#include<iostream>
int main(){
    std::cout << "Hello" << std::endl;
}

Output:

Cout and endl keys will be belonging to the std namespace.

6. Referring to a Class inside having Another Class

Code:

#include<iostream>
using namespace std;
class out_side{
public:
int xyzz;
class place_inside{
public:
int xyzz;
static int yzz; 
int foomo();
};
};
int out_side::place_inside::yzz = 5; 
int main(){
outside A;
outside::inside B;
}


Output:

//to refer to the class inside another no type no output here

this Pointer in C++

To know well about This operator in C++, we will look at objects and their orientation in C++; things get the identical or exact copy of their member class, and a code segment using the function will be called multiple times. This could be the reference of the code segment than the standard pointer reference, but the reference was not present in the early version of the C++ programming language.

Syntax (to return the calling object)

Test& Test::func (){
   //processing of the sum happens here
   return *this;
}

The below code depicts the case where a local variable name is as same as a member name

Code:

#include<iostream>
using namespace std;
class Test{
private:
   int xyxx;
public:
   void setX (int xyxx){
this->xyxx = xyxx;
   }
void print() { cout << "xyxx = " << xyxx << endl; }
};
int main(){
   Test obj;
   int xyxx = 20;
   obj.setX(xyxx);
   obj.print();
   return 0;}

Output:

xyxx = 20

In an instance where the reference to a local variable has been returned, the reference can be made to form chains of function calls on a unique single object copy of it.

Code:

#include<iostream>
using namespace std;
class Test{
private:
  int xyxx;
  int yxx;
public:
  Test(int xyxx = 0, int yxx = 0) { this->xyxx = xyxx; this->yxx = yxx; }
  Test &setX(int ab) { xyxx = ab; return *this; }
  Test &setY(int bc) { yxx = bc; return *this; }
  void print() { cout << "xyxx = " << xyxx << " yxx = " << yxx << endl; }
};
int main()
{
Test obj1(5, 5);
obj1.setX(10).setY(20);
obj1.print();
  return 0;
}

Output:

xyxx = 10 yxx = 20

Related Topics

C++ Program to find the largest number formed from an array

Given an array, write a program to find the largest number that will be formed from the elements of the array. Arrangement should be done in such a way that...

4 minutes read.

Bits stdc++.h in C++

<bits/stdc++.h> in C++ In essence, it is a header file that contains all the standard libraries. It makes sense to use this file in programming competitions to speed up work, especially...

2 minutes read.

C++ storage classes

Storage Classes are used to characterize a variable's or function's characteristics. These characteristics include scope, visibility, and life-time, which allow us to track the presence of a variable over the...

5 minutes read.

Virtual base class in C++

Consider in a C++ program, there are 4 classes named class A, class B, class C, and class D. If class B and class c inherit properties from class A....

3 minutes read.

Passing by Reference Vs. Passing by the pointer in C++

 Passing by Reference Vs. Passing by the pointer in C++ Throughout C++, it can transfer parameter values except by pointers or through referring to a function. For both cases, we have...

3 minutes read.

Queue in C++

What is Queue? As the name suggests, the queue is the type of data structure that follows the FIFO (First In - First Out) mechanism. In simple words, it is...

4 minutes read.

C++ Date and Time

The date and time formats in C++ will be covered in this article. Because C++ lacks a proper date and time format, we must rely on the c language. The...

7 minutes read.

Hybrid Inheritance

C++ Hybrid Inheritance When more than one type of inheritance is combined in single inheritance is called as hybrid inheritance. C++ Hybrid Inheritance Example #include<iostream>   using namespace std;   class Student{       protected:           int rollno;       public:           void getRoll(int a){               rollno=a;           }           void putRoll(void){               cout <<"Roll No: "<< rollno<<endl;           }   };   class Test : public Student{       protected:           float subject1, subject2;       public:           void getMarks(float x, float y){               subject1=x;               subject2=y;           }           void putMarks(void){               cout<< "Marks gain: "<<endl <<"Subject1 =  "<<subject1<<endl<<"Subject2 = "<<subject2 <<endl;           }   };   class Sport{       protected:           float score;       public:           void getScore(float s){               score=s;           }           void putScore(void){               cout<<"Sports score: "<<score<<endl;           }   };   class Result : public Test, public Sport{       float total;       public:           void display(void);   };   void Result:: display(void){       total=subject1+subject2+score;       putRoll();       putMarks();       putScore();       cout<<"Total Score: "<<total<<endl;   }   int main(){       Result stu;       stu.getRoll(10);       stu.getMarks(40,50);       stu.getScore(60);       stu.display();       return 0;   } Output: Roll No: 10 Marks gain: Subject1 = 40 Subject2 = 50 Sports score: 60 Total...

1 minute read.

How to Setup Environment for C++ Programming on Mac

Mac OS X code Installation There are so many environments available for C++. We are going to install jGrasp and Xcode in our mac operating system. Instruction for installation of jGrasp and...

2 minutes read.

C++ Inheritance

What do you mean by Inheritance ? The ability to define new classes based on existing classes in order to reuse and organise code is referred to as inheritance. Single inheritance...

7 minutes read.

Decimal to Hexadecimal in C++

We need to write a program in C++ that converts a decimal number into an equal hexadecimal number given a decimal value as input i.e. convert a number having a...

2 minutes read.

C++ If

C++ Control Statement C++ control statement or decision-making statement is used to control the flow of program statement according to condition applied. C++ if Control Statement An if control statement in C++ is used to...

2 minutes read.

Inheritance and Friendship in C++

In this tutorial, we will look into what Inheritance and Friendship in C++ are, as well as the differences between the two. What is Inheritance in C++: In C++, inheritance is an...

2 minutes read.

Dynamic Binding in C++

The notion of dynamic binding solved the challenges associated with static binding. Static binding refers to bindings that can be resolved by the compiler at runtime. All storage, stationary, and...

3 minutes read.

Palindrome Using While Loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

getline() Function and Character Array in C++

In this article, we will explore about some concepts on getline() function and character array in the most useful language C++. The getline() method in C++ is simply a standard library...

6 minutes read.

Implementing the sets without C++ STL containers

Many practical features and tools in C++ support us in programming competitions. One of these parts is a set from the Standard Template Library (STL), which offers an effective way...

6 minutes read.

While Loop Examples in C++

While Loop A while loop repeats all code in its body, also known as a while statement, as long as a specific condition is satisfied. The loop ends if or when...

4 minutes read.

Malloc() and new in C++

In C ++, malloc () and new are used for the same thing. During runtime, they are used to allocate memory. Malloc () and the new, on the other hand,...

4 minutes read.

C++ Program to find the product array puzzle

Write a C++ program to form a product array from arr[] where product[i] is the product of all the array elements except arr[i]. Example Input: arr[]  = {10, 3, 5, 6,...

6 minutes read.