Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • 🛣️C Plus Plus: Docs
  • Build
    • 🔥Build Process
    • 🔧Connect multiple C++ Files
  • Code
    • ⏮️Pre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • 🪅Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • 🎭Casting
    • 🔃Operator overloading
      • Available Operators
      • Examples of operator overloading
    • 🗺️Namespace
      • Namespace Example
      • Using directive
    • 🕵️Header File
      • For C++ Classes
    • 🏗️Structure
      • Struct vs Class
      • Public vs Private inheritance
    • 🏢Classes
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • 🧠Memory Introduction
    • ✨Heap and Stack
    • 🎯Pointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • 👨‍🏭Helper function
    • 🍡Vector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • ♻️Union
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • 🧵Threading
      • std::thread
      • Detach a thread
  • Misc
    • 🗂️Execution Order
    • 🧠Print memory
Powered by GitBook
On this page
  1. Memory
  2. Pointers

'this' Pointer

The this pointer is a special pointer in C++ that points to the current instance of a class. It's an implicit parameter to all non-static member functions and has several important uses and characteristics.

#include <iostream>
#include <string>

class Base {
protected:
    int value;

public:
    Base(int v) : value(v) {}

    // Using 'this' in operator overloading
    Base& operator+=(int x) {
        this->value += x;
        return *this;
    }

    // Virtual function using 'this'
    virtual void print() const {
        std::cout << "Base: " << this->value << std::endl;
    }

    // Using 'this' to call another member function
    void increment() {
        this->add(1);
    }

private:
    void add(int x) {
        value += x;
    }
};

class Derived : public Base {
private:
    std::string name;

public:
    Derived(int v, const std::string& n) : Base(v), name(n) {}

    // Using 'this' to access both base and derived members
    void print() const override {
        std::cout << "Derived: " << this->name << ", " << this->value << std::endl;
    }

    // Using 'this' to disambiguate between base and derived class methods
    void test() {
        this->Base::print();  // Call base class print
        this->print();        // Call derived class print
    }

    // Using 'this' in a template method
    template<typename T>
    void compareAndPrint(const T& other) const {
        if (this->value > other.value) {
            std::cout << this->name << " is greater" << std::endl;
        } else {
            std::cout << other.name << " is greater or equal" << std::endl;
        }
    }
};

int main() {
    Base b(10);
    b += 5;
    b.print();

    Derived d1(20, "Object1");
    Derived d2(30, "Object2");

    d1.test();

    d1 += 15;
    d1.print();

    d1.compareAndPrint(d2);

    // Using 'this' in polymorphism
    Base* ptr = &d1;
    ptr->print();  // Calls Derived::print()

    return 0;
}
PreviousDangling PointerNextFunction Pointer

Last updated 9 months ago

Run it .

🎯
here