The Wayback Machine - https://web.archive.org/web/20240828110659/https://www.geeksforgeeks.org/scope-resolution-operator-vs-this-pointer-in-cpp/
Open In App

Scope Resolution Operator vs this pointer in C++

Last Updated : 28 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

Consider below C++ program:  

CPP




// C++ program to show that local parameters hide
// class members
#include <iostream>
using namespace std;
  
class Test {
    int a;
  
public:
    Test() { a = 1; }
  
    // Local parameter 'a' hides class member 'a'
    void func(int a) { cout << a; }
};
  
// Driver Code
int main()
{
    Test obj;
    int k = 3;
    obj.func(k);
    return 0;
}


Output

3

Explanation: The output for the above program is 3 since the “a” passed as an argument to the func shadows the “a” of the class .i.e 1 

Then how to output the class’s ‘a’. This is where this pointer comes in handy. A statement like cout <<this->a instead of cout << a can simply output the value 1 as this pointer points to the object from where func is called.

CPP




// C++ program to show use of this to access member when
// there is a local variable with same name
#include <iostream>
using namespace std;
class Test {
    int a;
  
public:
    Test() { a = 1; }
  
    // Local parameter 'a' hides object's member
    // 'a', but we can access it using this.
    void func(int a) { cout << this->a; }
};
  
// Driver code
int main()
{
    Test obj;
    int k = 3;
    obj.func(k);
    return 0;
}


Output

1

How about Scope Resolution Operator? 

We cannot use the scope resolution operator in the above example to print the object’s member ‘a’ because the scope resolution operator can only be used for a static data member (or class members). If we use the scope resolution operator in the above program we get compiler error and if we use this pointer in the below program, then also we get a compiler error.

CPP




// C++ program to show that scope resolution operator can be
// used to access static members when there is a local
// variable with same name
#include <iostream>
using namespace std;
  
class Test {
    static int a;
  
public:
    // Local parameter 'a' hides class member
    // 'a', but we can access it using ::
    void func(int a) { cout << Test::a; }
};
  
// In C++, static members must be explicitly defined
// like this
int Test::a = 1;
  
// Driver code
int main()
{
    Test obj;
    int k = 3;
    obj.func(k);
    return 0;
}


Output

1


Previous Article
Next Article

Similar Reads

Scope resolution operator in C++
In C++, the scope resolution operator is ::. It is used for the following purposes. 1) To access a global variable when there is a local variable with same name: [GFGTABS] CPP // C++ program to show that we can access a global variable // using scope resolution operator :: when there is a local // variable with same name #include&lt;iostream&gt; us
4 min read
C++ Pointer To Pointer (Double Pointer)
In C++ a Pointer is a variable that is used to store the memory address of other variables. It is a variable that points to a data type (like int or string) of the same type and is created with the * operator. Syntax of a Pointer in C++: data_type_of_pointer *name_of_variable = &amp; normal_variable; What is a Pointer to a Pointer or Double Pointer
4 min read
C - Pointer to Pointer (Double Pointer)
Prerequisite: Pointers in C The pointer to a pointer in C is used when we want to store the address of another pointer. The first pointer is used to store the address of the variable. And the second pointer is used to store the address of the first pointer. That is why they are also known as double-pointers. We can use a pointer to a pointer to cha
5 min read
Difference between Dangling pointer and Void pointer
Dangling pointer: A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer. There are three different ways where Pointer acts as a dangling pointer: By deallocating memoryFunction CallWhen the variable goes out of scope Void pointer: Void pointer is a specific pointer type – void * – a pointer that point
2 min read
Pointer to an Array | Array Pointer
Prerequisite: Pointers Introduction  Consider the following program:  [GFGTABS] C #include&lt;stdio.h&gt; int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr; printf(&quot;%p\n&quot;, ptr); return 0; } [/GFGTABS]In the above program, we have a pointer ptr that points to the 0th element of the array. Similarly, we can also declare a pointer
9 min read
deque::operator= and deque::operator[] in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::operator= This operator
4 min read
Operator Overloading '&lt;&lt;' and '&gt;&gt;' operator in a linked list class
Prerequisite: Operator Overloading in C++, Linked List in C++ C++ comes with libraries that provide ways for performing Input and Output. In C++, Input and Output are performed as a sequence of bytes, also known as streams. Input and Output stream are managed by the iostream library. cin and cout are the standard objects for the input stream and ou
4 min read
Why overriding both the global new operator and the class-specific operator is not ambiguous?
The below section deals about overload resolution as it helps in the fundamentals of overloading and overriding. Predict the output: C/C++ Code #include &lt;iostream&gt; using namespace std; class Gfg { public: void printHello() { cout &lt;&lt; &quot;hello gfg-class specific&quot; &lt;&lt; endl; } }; void printHello() { cout &lt;&lt; &quot;hello gf
5 min read
vector::operator= and vector::operator[ ] in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::operator=This operator is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new
4 min read
C | Variable Declaration and Scope | Question 1
Consider the following two C lines int var1; extern int var2; Which of the following statements is correct (A) Both statements only declare variables, don't define them. (B) First statement declares and defines var1, but second statement only declares var2 (C) Both statements declare define variables var1 and var2 Answer: (B) Explanation: See Under
1 min read
C | Variable Declaration and Scope | Question 2
Predict the output #include &lt;stdio.h&gt; int var = 20; int main() { int var = var; printf(&quot;%d &quot;, var); return 0; } (A) Garbage Value (B) 20 (C) Compiler Error Answer: (A) Explanation: First var is declared, then value is assigned to it. As soon as var is declared as a local variable, it hides the global variable var.Quiz of this Questi
1 min read
C | Variable Declaration and Scope | Question 3
#include &lt;stdio.h&gt; extern int var; int main() { var = 10; printf(&quot;%d &quot;, var); return 0; } (A) Compiler Error: var is not defined (B) 20 (C) 0 Answer: (A) Explanation: var is only declared and not defined (no memory allocated for it) Refer: Understanding “extern” keyword in CQuiz of this Question
1 min read
C | Variable Declaration and Scope | Question 4
#include &lt;stdio.h&gt; extern int var = 0; int main() { var = 10; printf(&quot;%d &quot;, var); return 0; } (A) 10 (B) Compiler Error: var is not defined (C) 0 Answer: (A) Explanation: If a variable is only declared and an initializer is also provided with that declaration, then the memory for that variable will be allocated i.e. that variable wi
1 min read
C | Variable Declaration and Scope | Question 6
Output? #include &lt;stdio.h&gt; int main() { int x = 1, y = 2, z = 3; printf(&quot; x = %d, y = %d, z = %d \n&quot;, x, y, z); { int x = 10; float y = 20; printf(&quot; x = %d, y = %f, z = %d \n&quot;, x, y, z); { int z = 100; printf(&quot; x = %d, y = %f, z = %d \n&quot;, x, y, z); } } return 0; } (A) x = 1, y = 2, z = 3 x = 10, y = 20.000000, z
1 min read
C | Variable Declaration and Scope | Question 7
int main() { int x = 032; printf(&quot;%d&quot;, x); return 0; } (A) 32 (B) 0 (C) 26 (D) 50 Answer: (C) Explanation: When a constant value starts with 0, it is considered as octal number. Therefore the value of x is 3*8 + 2 = 26Quiz of this Question
1 min read
C | Variable Declaration and Scope | Question 8
Consider the following C program, which variable has the longest scope? int aÍū int main() { int bÍū // .. // .. } int cÍū (A) a (B) b (C) c (D) All have same scope Answer: (A) Explanation: a is accessible everywhere. b is limited to main() c is accessible only after its declaration.Quiz of this Question
1 min read
Scope of Variables in C++
In general, the scope is defined as the extent up to which something can be worked with. In programming also the scope of a variable is defined as the extent of the program code within which the variable can be accessed or declared or worked with. There are mainly two types of variable scopes: Local VariablesGlobal Variables Now let's understand ea
5 min read
Scope rules in C
The scope of a variable in C is the block or the region in the program where a variable is declared, defined, and used. Outside this region, we cannot access the variable and it is treated as an undeclared identifier. The scope is the area under which a variable is visible.The scope of an identifier is the part of the program where the identifier m
6 min read
How to declare a pointer to a function?
While a pointer to a variable or an object is used to access them indirectly, a pointer to a function is used to invoke a function indirectly. Well, we assume that you know what it means by a pointer in C. So how do we create a pointer to an integer in C? Huh..it is pretty simple... int *ptrInteger; /*We have put a * operator between int and ptrInt
2 min read
Pointer vs Array in C
Most of the time, pointer and array accesses can be treated as acting the same, the major exceptions being: 1. the sizeof operator sizeof(array) returns the amount of memory used by all elements in the array sizeof(pointer) only returns the amount of memory used by the pointer variable itself 2. the &amp; operator array is an alias for &amp;array[0
1 min read
Type of 'this' Pointer in C++
In C++, this pointer refers to the current object of the class and passes it as a parameter to another method. 'this pointer' is passed as a hidden argument to all non-static member function calls. Type of 'this' pointer The type of this depends upon function declaration. The type of this pointer is either const ExampleClass * or ExampleClass *. It
2 min read
How to write C functions that modify head pointer of a Linked List?
Consider simple representation (without any dummy node) of Linked List. Functions that operate on such Linked lists can be divided into two categories: 1) Functions that do not modify the head pointer: Examples of such functions include, printing a linked list, updating data members of nodes like adding given a value to all nodes, or some other ope
4 min read
NULL Pointer in C
The Null Pointer is the pointer that does not point to any location but NULL. According to C11 standard: “An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guarantee
4 min read
C | Pointer Basics | Question 1
What is the output of following program? # include &lt;stdio.h&gt; void fun(int x) { x = 30; } int main() { int y = 20; fun(y); printf(&quot;%d&quot;, y); return 0; } (A) 30 (B) 20 (C) Compiler Error (D) Runtime Error Answer: (B) Explanation: Parameters are always passed by value in C. Therefore, in the above code, value of y is not modified using
1 min read
C | Pointer Basics | Question 2
Output of following program? # include &lt;stdio.h&gt; void fun(int *ptr) { *ptr = 30; } int main() { int y = 20; fun(&amp;y); printf(&quot;%d&quot;, y); return 0; } (A) 20 (B) 30 (C) Compiler Error (D) Runtime Error Answer: (B) Explanation: The function fun() expects a pointer ptr to an integer (or an address of an integer). It modifies the value
1 min read
C | Pointer Basics | Question 3
Output of following program? #include &lt;stdio.h&gt; int main() { int *ptr; int x; ptr = &amp;x; *ptr = 0; printf(&quot; x = %d\n&quot;, x); printf(&quot; *ptr = %d\n&quot;, *ptr); *ptr += 5; printf(&quot; x = %d\n&quot;, x); printf(&quot; *ptr = %d\n&quot;, *ptr); (*ptr)++; printf(&quot; x = %d\n&quot;, x); printf(&quot; *ptr = %d\n&quot;, *ptr);
2 min read
C | Pointer Basics | Question 4
Consider a compiler where int takes 4 bytes, char takes 1 byte and pointer takes 4 bytes. #include &lt;stdio.h&gt; int main() { int arri[] = {1, 2 ,3}; int *ptri = arri; char arrc[] = {1, 2 ,3}; char *ptrc = arrc; printf(&quot;sizeof arri[] = %d &quot;, sizeof(arri)); printf(&quot;sizeof ptri = %d &quot;, sizeof(ptri)); printf(&quot;sizeof arrc[] =
1 min read
C | Pointer Basics | Question 17
Assume that float takes 4 bytes, predict the output of following program. #include &lt;stdio.h&gt; int main() { float arr[5] = {12.5, 10.0, 13.5, 90.5, 0.5}; float *ptr1 = &amp;arr[0]; float *ptr2 = ptr1 + 3; printf(&quot;%f &quot;, *ptr2); printf(&quot;%d&quot;, ptr2 - ptr1); return 0; } (A) 90.500000 3 (B) 90.500000 12 (C) 10.000000 12 (D) 0.5000
1 min read
C | Pointer Basics | Question 6
#include&lt;stdio.h&gt; int main() { int arr[] = {10, 20, 30, 40, 50, 60}; int *ptr1 = arr; int *ptr2 = arr + 5; printf(&quot;Number of elements between two pointer are: %d.&quot;, (ptr2 - ptr1)); printf(&quot;Number of bytes between two pointers are: %d&quot;, (char*)ptr2 - (char*) ptr1); return 0; } Assume that an int variable takes 4 bytes and a
2 min read
C | Pointer Basics | Question 7
#include&lt;stdio.h&gt; int main() { int a; char *x; x = (char *) &amp;a; a = 512; x[0] = 1; x[1] = 2; printf(&quot;%d\n&quot;,a); return 0; } What is the output of above program? (A) Machine dependent (B) 513 (C) 258 (D) Compiler Error Answer: (A) Explanation: Output is 513 in a little endian machine. To understand this output, let integers be sto
1 min read
Practice Tags :