Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Scope Resolution Operator in C++
3.
this pointer in C++
4.
Scope Resolution Operator vs this pointer
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Scope resolution operator vs this pointer

Author Teesha Goyal
0 upvote

Introduction

To understand the scope resolution operator vs this pointer, we will first separately discuss the scope resolution operator in C++ and how to use it. We will discuss this pointer in C++ and its uses. And at last, we will discuss the scope resolution operator vs this pointer.

Also, see Literals in C, Fibonacci Series in C++

Scope Resolution Operator in C++

Scope resolution operator in C++ is written as two colon symbols(::). It is used to access static or class members of a class in C++.

If we have a function that has a local variable name, same as the name of a static member, and we access the variable by name, then the local variable has the precedence, and it shadows the static variable. The value of the local variable is printed. So, to access the static variable, we have to use the scope resolution operator(::) with the class name in the given syntax.

class_name :: static_variable_name
You can also try this code with Online C++ Compiler
Run Code


Following is an example of how we can use the scope resolution operator in C++ to access the static members or class members.

#include<iostream>
using namespace std;

class Demo{
    //class member var
    static int var;
   
    public:
    //A function that prints the value of var
    void demo_func(int var){
        cout<<Demo::var<<endl;  //By using :: we can access the static variable
        cout<<var;  //This will print the local variable
    }
};

//It is necessary to explicitly define static member in C++
int Demo::var = 75;

//Driver function
int main(){

    Demo obj;
    obj.demo_func(10);

    return 0;
}
You can also try this code with Online C++ Compiler
Run Code


Output

75
10

 

You can try and compile with the help of online c++ compiler.

this pointer in C++

The this pointer in C++ is used to access the object variables in a function when we have the local variables with the same name. Local variables have precedence over object variables, so when we access a variable by name local variable is accessed. So, to access the object variables, we have to use this pointer. this pointer is used to point to the current object used to invoke the function.

The syntax to use this pointer in c++ is as follows:

this -> variable_name
You can also try this code with Online C++ Compiler
Run Code

 

Following is an example of how we can use this pointer in C++ to access the object members in C++.

#include<iostream>
using namespace std;

class Demo{
    int var;
   
    public:
    //Constructor that sets the value of var for each object.
    Demo(int x){
        var = x;
    }

    //A function that prints the value of var
    void demo_func(int var){
        cout<<this->var<<endl;  //By using this->variable_name we can access the object variable
        cout<<var;  //This will print the local variable
    }
};


//Driver function
int main(){

    Demo obj(100);
    obj.demo_func(10);

    return 0;
}
You can also try this code with Online C++ Compiler
Run Code


Output

100
10

Scope Resolution Operator vs this pointer

The scope resolution operator(::) in C++ is used to access the class members or static members of a class when we have a local variable present with the same name. 

In contrast, this pointer in C++ is used to access the object variable when a local variable with the same name is present in a function.

FAQs

  1. What is the purpose of using a scope resolution operator in C++?
    The purpose of using a scope resolution operator in C++ is to access the class members or static variables in C++.
     
  2. What is the symbol of the scope resolution operator in C++?
    The symbol for scope resolution operator in C++ is two colon symbols(::).
     
  3. What are static members in C++?
    Static members are also called class members because they are the property of a class and not an object. They are shared among all the objects of a class.
     
  4. What are formal and actual parameters in C++?
    Formal parameters are the local variables that are declared in function declarations, and actual parameters are the variables that are passed as an argument to a function at the function call.
     
  5. What is the purpose of this pointer in C++?
    The “this” pointer points to the invoking object, and so it is used to denote the current object that is used to invoke the function.

Key Takeaways

In this article, We discussed the scope resolution operator vs this pointer in C++. We discussed how the scope resolution operator is different from the “this” point.

Are you planning to ace the interviews of reputed product-based companies like Amazon, Google, Microsoft, and more? 

Attempt our Online Mock Test Series on Coding Ninjas Studio now!
 

Happy Coding!

Live masterclass