×

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

Dynamic _Cast in C++

C++ is one of the most powerful programming languages. We can write object-oriented and structured programming with the help of C++. In this article, we will learn about the dynamic...

3 minutes read.

C++ Goto

In this article, we will discuss the C++ goto statement with its syntax, use, key features, key points, pseudo code, and examples. What is the goto statement in C++? In C++, the...

4 minutes read.

rand() and srand() in C / C++

In this tutorial, we'll explore the syntax, usage, and examples of the C++ STL functions rand() and srand(). What exactly is rand()? The C++ STL's built-in rand() function is defined in the...

3 minutes read.

C++ Variable

In this article, we will discuss variables in C++ with their types and examples. What are Variables? Variables are specific memory storage spaces that hold a value. During the execution of a...

4 minutes read.

C++ Try-Catch

Every useful program will eventually encounter unexpected outcomes. By entering data that are incorrect, users might create mistakes. Sometimes the program's creator didn't consider all of the options or was...

7 minutes read.

Accumulate() and partial_sum() in C++ STL Numeric header

The C++ STL's numeric library includes the numeric header. This library provides efficient numeric arrays, support for random number generation, and fundamental mathematical operations and types. Several of the numeric...

3 minutes read.

C++ Close file

C++ File Handling close() Function A file which is opened while reading or writing in file handling must be closed after performing an action on it. The close() function is used to close...

2 minutes read.

Free vs delete() in C++

Free vs delete() in C++ In this section, we will learn about the free() function and also create a C ++ program of the delete operator. What is free() Function in C++? In...

4 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

Palindrome using Do-while loop in C++

What is Palindrome? 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++...

5 minutes read.

C++ Bitwise XOR Operator

Exclusive OR is another name for the bitwise XOR operator. The ‘^’ is used to indicate it. It operates at the bit level of the operands, as the name implies....

4 minutes read.

C++ Algorithms

There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of...

5 minutes read.

Const_cast in C++ Type Casting Operators

In this tutorial, we will learn enough about const cast in the most widely used programming language, C++, as well as type casting operations. C++ supports the four types of casting...

4 minutes read.

C++ Program to Implement Merge Sort

C++ Program to Implement Merge Sort The technique of merge sort is based on the strategy of divide and conquer. We divide the set of while data into smaller bits, arranged...

3 minutes read.

Array of Object in C++

What is an Array? In C++, an array is a group of related data types, such as int, char, float, double, etc., that are easily accessed by index value alone and...

12 minutes read.

C++ Program to Print Fibonacci Triangle

Fibonacci Triangle Program in CPP Definition: Fibonacci Triangle as the name suggests is the same as the Fibonacci number series where the next element is the sum of the previous two elements....

3 minutes read.

Size_t Data Type in C++

In C++, the type to express the object size in bytes is defined as Size_t, an unsigned integer type offered by the standard library for describing the object's size and...

3 minutes read.

Learn C++ Tutorial

C++ Introduction C++ is an object-oriented programming language. It was developed by Bjarne Stroustrup at AT&T Bell Laboratories. It is superset (extension) of C programming language. Depending upon features supported by programming...

10 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.

Copy elision in C++

The Copy Omission is another name for the Copy Elision. One of the several compiler optimization techniques is copy elision. It prevents items from being copied inadvertently. This Copy Elision approach...

3 minutes read.