StackStalk
  • Home
  • Java
    • Java Collection
    • Spring Boot Collection
  • Python
    • Python Collection
  • C++
    • C++ Collection
    • Progamming Problems
    • Algorithms
    • Data Structures
    • Design Patterns
  • General
    • Tips and Tricks

Friday, May 30, 2008

C++ this pointer

 May 30, 2008     CPP     No comments   

What is this pointer?

  • Every object has a special pointer "this" which points to the object itself.
  • This pointer is accessible to all members of the class but not to any static members of the class.
  • Can be used to find the address of the object in which the function is a member.
  • Presence of this pointer is not included in the sizeof calculations.

Demonstrate the usage of C++ this pointer

#include <iostream>
using namespace std;

class MyClass {
        int data;
    public:
        MyClass() {data=100;};
        void Print1();
        void Print2();
};

// Not using this pointer
void MyClass::Print1() {
    cout lt;lt; data lt;lt; endl;
}

// Using this pointer
void MyClass::Print2() {
    cout << "My address = " << this << endl;
    cout << this->data << endl;
}


int main()
{
    MyClass a;
    a.Print1();
    a.Print2();

    // Size of doesn't include this pointer
    cout << sizeof(a) << endl;
}
OUTPUT:
100
My address = 0012FF88
100
4

Practical use. Use this pointer to resolve ambiguity

#include <iostream>
using namespace std;

class MyClass {
        int data;
    public:
        void SetData(int data);
        int GetData() { return data; };
};

// Same name for function argument and class member
// this pointer is used to resolve ambiguity
void MyClass::SetData(int data) {
    this->data = data;
}

int main()
{
    MyClass a;
    a.SetData(100);
    cout << a.GetData() << endl;
}

Practical use. In assignment operators to reduce memory usage

#include <iostream>
using namespace std;

class MyClass {
        int data1;
        int data2;
    public:
        MyClass(int data1, int data2) {
            this->data1 = data1;
            this->data2 = data2;
        }

    /* Return by value
    MyClass operator = ( MyClass& c ) {
        this->data1 = c.data1;
        this->data2 = c.data2;
        return MyClass(100, 200);
    }
    */

    // Return by reference. Less memory usage
    MyClass& operator = ( MyClass& c ) {
        this->data1 = c.data1;
        this->data2 = c.data2;
        return *this;
    }

    void Print() {
        cout << data1 << endl;
        cout << data2 << endl;
    }
};

int main() 
{
    MyClass obj1(100, 200);
    MyClass obj2 = obj1;
    obj2.Print();
}
OUTPUT:
100
200
  • Share This:  
Newer Post Older Post Home

0 comments:

Post a Comment

Follow @StackStalk
Get new posts by email:
Powered by follow.it

Popular Posts

  • Avro Producer and Consumer with Python using Confluent Kafka
    In this article, we will understand Avro a popular data serialization format in streaming data applications and develop a simple Avro Produc...
  • Monitor Spring Boot App with Micrometer and Prometheus
    Modern distributed applications typically have multiple microservices working together. Ability to monitor and manage aspects like health, m...
  • Server-Sent Events with Spring WebFlux
    In this article we will review the concepts of server-sent events and work on an example using WebFlux. Before getting into this article it ...
  • Implement caching in a Spring Boot microservice using Redis
    In this article we will explore how to use Redis as a data cache for a Spring Boot microservice using PostgreSQL as the database. Idea is to...
  • Python FastAPI microservice with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Spring Boot with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Getting started with Kafka in Python
    This article will provide an overview of Kafka and how to get started with Kafka in Python with a simple example. What is Kafka? ...
  • Getting started in GraphQL with Spring Boot
    In this article we will explore basic concepts on GraphQL and look at how to develop a microservice in Spring Boot with GraphQL support. ...

Copyright © StackStalk