Scope Resolution Operator vs this operator in C++

Scope Resolution Operator vs this operator

On this page we will discuss about difference between scope resolution operator and this operator in C++. Scope resolution operator is for accessing static or class members and this pointer is for accessing object members when there is a local variable with the same name.

Scope Resolution Operator vs this operator in C++

Scope Resolution Operator

The scope resolution operator is denoted by (::), and it is placed before the name of the variable, function, or class member to which you want to refer.

In C++, the scope resolution operator is used to refer to a global variable or function that has been hidden by a local variable or function with the same name, and also to access members of a class from outside the class.

Syntax

 ::var_name
where var_name is the name of the variable, function, or class member that you want to refer to.

this operator

In C++, the “this” operator is a operator that is passed implicitly to non-static member functions, and it points to the object that the member function is being invoked on. It is used to access members of the class from within the class, and to distinguish between class members and local variables that have the same name.

The “this” operator is a constant operator, and its type is a operator to the class in which the member function is defined. It is available only in non-static member functions, and it is not available in static member functions or in global functions, since these functions do not have access to the object on which they are called.

Syntax

To use the “this” pointer within a non-static member function, you simply use the keyword “this” to refer to the current object on which the function is being invoked. For example, if you have a class named MyClass, and you want to access a member variable x from within a member function, you would use the following syntax:
this->x

The -> operator is used to access member of a class through a pointer.

Example of Scope Resolution Operator

Run
#include <iostream>
using namespace std;

// global variable
int x = 0;

// global function
void printX()
{
  cout << "Global x = " << x << endl;
}

class MyClass
{
public:
  // class variable
  static int x;

  // class function
  static void printX()
  {
    cout << "MyClass x = " << x << endl;
  }
};
int MyClass::x = 10;

int main()
{
  // local variable
  int x = 5;
  cout << "Local x = " << x << endl;

  // using :: operator to refer to global x
  ::x = 7;
  ::printX();

  // using :: operator to refer to static class variable
  MyClass::x = 12;
  MyClass::printX();
  return 0;
}

Output:

Local x = 5
Global x = 7
MyClass x = 12

Example of this operator

Run
#include <iostream>
using namespace std;

class MyClass
{
private:
  int x;
public:
    MyClass(int x): x(x) {}
  void setX(int x)
  {
    this->x = x;
  }
  void print()
  {
    cout << "Value of x : " << x << endl;
    cout << "Address of the object : " << this << endl;
  }
};

int main()
{
  MyClass obj(5);
  obj.print();
  obj.setX(10);
  obj.print();
  return 0;
}

Output:

Value of x : 5
Address of the object : 0x7ffe0b9e8c74
Value of x : 10
Address of the object : 0x7ffe0b9e8c74

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription