×

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

How to build a program in C++

Building a program is all about creating the program and executing it successfully. There are some steps  precisely, which must be followed to make the program. Step 1: Get an IDE...

4 minutes read.

Swap numbers in C++

Swap numbers Swapping refers to interchanging values between two variables. Swapping is important and easy to understand programming logic in the world of coding. Though it is used in the programming...

4 minutes read.

Initialize Array of objects with parameterized constructors in C++

Initialize Array of objects with parameterized constructors in C++ When a class is defined, only the specification for the object is specified; no memory or capacity is allocated. You need to...

3 minutes read.

Preventing Object Copy in C++

C++ is an object-oriented programming language that provides the ability to create objects, define class and pass objects to functions. When passing an object to a function or returning an...

7 minutes read.

Splitting a string in C++

Any programming language must have the ability to work with string data. For programming needs, we sometimes need to separate string data. Many computer languages provide a split() method that...

4 minutes read.

C++ History

C++ is a middle-level programming language developed in 1980s by Bjarne Stroustrup at Bel Labs. C ++ development initially started in 1979, four years before its launch. It is started with...

1 minute read.

How to calculate size of string in C++

What is string in C++? In C++, a string is a sequence of characters. The string data type is part of the Standard Template Library (STL) and is defined in the...

4 minutes read.

Reverse an Array in C++

The many approaches to reverse an array in the C++ programming language will be discussed in this section. The term "reverse of an array" refers to changing the order of...

8 minutes read.

Quick Sort in C++

Quick sort is an efficient, in-place, comparison-based sorting algorithm that uses a divide-and-conquer strategy to sort an array or list of elements. First a pivot element is selected from the...

4 minutes read.

C++ Virtual Destructor

In C++, a destructor is a class member function that is used to free up space or remove an object of the class that has gone out of scope. The...

4 minutes read.

Dynamic Memory Allocation in C++

In some programming situations, the number of data items changes as the program is running, which is known as dynamic data or input. Consider a real-world situation where a program...

3 minutes 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.

What does Buffer Flush mean in C++

A buffer flush, to explain simple layman's terms, is nothing but the transfer of computer data which is being stored in a rentable temporary memory of your computer running either...

3 minutes read.

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

C++ String

A string is a collection of characters. C++ programming language supports both C string as well as standard C++ library string. In C++, string is an object of std::string class. C Style String The C style...

5 minutes read.

Find the Size of Array in C/C++ without using sizeof() function

We know that arrays in C/C++ are the most essential data structures as they have the ability to hold the data in a continuous manner line where the address of...

3 minutes read.

Const Keyword in C++

The const programming language keyword will be covered in this section. The constant value that cannot change during program execution is defined using the const keywords. It implies that once...

9 minutes read.

Types of polymorphism in C++

What is polymorphism in C++ ? Polymorphism literally translates to "multiple forms". This indicates that the same thing behaves differently depending on the context in programming. Polymorphism is a feature of C++...

6 minutes read.

C++ add two numbers using the function

Here we will learn how to add two numbers by creating function in C++. Let’s learn this with help of example. Code: - #include <iostream> using namespace std; int add_two_no(int a, int b); int main(){   int...

1 minute read.

C++ Math Functions

C++ Math Functions: Like other programming languages, C++ offers plenty of mathematical functions needed for various purposes. These functions are defined mainly in the math library in C++. Let us...

4 minutes read.