Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Scope Resolution Operator vs this pointer in C++
Both scope resolution operator and 'this' pointer are used for different purposes in the C++ programs. The scope resolution operator is used to access static or class members whereas this pointer is used to access object members when there is a local variable with the same name.
Scope Resolution Operator in C++
The concept of scope resolution operator (::) is used in Object-Oriented Programming (OOPs) and namespace standard that can used to access something belongs to a specific scope, such as a class or namespace.
Syntax
It's syntax is as follows:
::
Example
In this program, we use the :: scope resolution operator with a static class member, which demonstrates how this operator references the class variable instead of the function parameter. If we do not use the :: operator, the local variable x can take the precedence. This shows how scope resolution works over variable referencing.
#include<iostream>
class AB {
static int x;
public:
// local parameter 'x' hides class member
// 'x', but we can access it using ::
void print(int x) {
std::cout << "The number is:" << AB::x;
}
};
// static members must be explicitly defined
int AB::x = 7;
int main() {
AB ob;
int m = 6 ;
ob.print(m);
return 0;
}
The above code produces the following result:
The number is: 6
'this' Pointer in C++
The 'this' pointer is also known implicit pointer that define the current instance of a class. It allows member functions to access the object attributes and methods without explicitly using the object address.
Syntax
The 'this' pointer has two main form of syntax as follows:
i. Referencing: It access a member variable or function of an object using this pointer.
this->member_name
ii. Dereferencing: It access the value stored at the memory address the pointer holds.
*this->member_name
Here,
- this/*this: *this dereferences this, that result the object itself.
- member_name: It is used to access the member.
Example 1
The following is the basic example to print the value using this pointer.
#include<iostream>
class TP {
public:
int value;
TP(int value) {
// Using 'this' pointer
this->value = value;
}
void display() {
std::cout << "Value: " << this->value << std::endl;
}
};
int main() {
TP obj(42);
obj.display();
return 0;
}
The above program produces the following result:
Value: 42
Example 2
In this example, we temporarily hide the local variable (x), which has a value of 7, but using this->x, we access the object of our own member variable x, which is 16. When we call the function print(m), the argument m is passed. This is how this-> is used and print the value of object member variable.
#include<iostream>
using namespace std;
class AB {
int x;
public:
AB() {
x = 16;
}
// here local parameter 'x' hides object's member
// 'x', we can access it using this
void print(int x) {
cout<<"the number is: " << this->x;
}
};
int main() {
AB ob;
int m = 7 ;
ob.print(m);
return 0;
}
The above code produces the following result:
The number is: 16
Difference between Scope Resolution Operator and 'this' Pointer
Here, we have a tabular differences of scope resolution and this pointer to show its importance:
| Scope Resolution Operator | this Pointer |
|---|---|
| It refers to access static members or solving the name conflict. | It refers to the current object in a class. |
| The symbol of scope resolution operator is ::. | There are two ways: referencing (this->member) and dereferencing (*this->member). |
| This is used with the class names. For eg. ClassName::member | It is used with non-static members. For eg. this->member |
| It is stored in static memory. | It is stored in heap or stack. |
| It can be used outside the definition. | It can be used only with non-static member functions. |
| This does not require an object. | It exists only when an object is created. |