Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Sunday, September 29, 2024

Pointer to Objects and the `this` Pointer in C++

 

Pointer to Objects and the `this` Pointer in C++

 

In C++, pointers and references to objects are fundamental concepts that enable dynamic memory management and object manipulation. The `this` pointer is a special pointer used within class member functions to refer to the object for which the member function is called.

 

1. Pointer to Objects

Definition: A **pointer to an object** is a variable that holds the memory address of an object. Using pointers to objects allows dynamic allocation, manipulation, and deallocation of objects during runtime.

Syntax:

ClassName* pointerName;

 

Example:

#include <iostream>

class Box {

public:

    double length;

    double width;

    double height;

 

    Box(double l, double w, double h) : length(l), width(w), height(h) {}

 

    void display() {

        std::cout << "Length: " << length << ", Width: " << width << ", Height: " << height << std::endl;

    }

};

 

int main() {

    // Creating an object dynamically

    Box* boxPtr = new Box(3.0, 4.0, 5.0);

 

    // Accessing members via pointer

    boxPtr->display();

 

    // Deleting the object to free memory

    delete boxPtr;

 

    return 0;

}

 

 

Explanation:

- `Box* boxPtr` declares a pointer to a `Box` object.

- `new Box(3.0, 4.0, 5.0)` dynamically allocates a `Box` object and returns a pointer to it.

- `boxPtr->display()` uses the arrow operator (`->`) to call the `display` method on the object pointed to by `boxPtr`.

- `delete boxPtr` frees the memory allocated for the object.

 

Key Points:

- Dynamic Allocation: Allows objects to be created at runtime and managed manually.

- Arrow Operator (`->`): Used to access members of an object through a pointer.

- Memory Management: Requires explicit deletion of objects to avoid memory leaks.

 

 2. The `this` Pointer

Definition: The `this` pointer is an implicit pointer available inside non-static member functions of a class. It points to the object for which the member function is called.

 

Syntax:

return_type function_name() {

    // Access members using this pointer

}

 

Example:

 

#include <iostream>

 

class Box {

public:

    double length;

    double width;

    double height;

 

    Box(double l, double w, double h) : length(l), width(w), height(h) {}

 

    // Method to compare if the current object is equal to another Box

    bool isEqual(const Box& other) {

        return (this->length == other.length &&

                this->width == other.width &&

                this->height == other.height);

    }

 

    // Method to display object details

    void display() {

        std::cout << "Length: " << this->length << ", Width: " << this->width << ", Height: " << this->height << std::endl;

    }

};

 

int main() {

    Box box1(3.0, 4.0, 5.0);

    Box box2(3.0, 4.0, 5.0);

 

    box1.display();

    std::cout << "Boxes are equal: " << (box1.isEqual(box2) ? "Yes" : "No") << std::endl;

 

    return 0;

}

 

Explanation:

- Inside `isEqual`, `this` points to the object for which `isEqual` was called (`box1` in this case).

- `this->length` accesses the `length` member of the current object.

- The `display` function uses `this` to refer to the current object’s members.

 

Key Points:

- Implicit Pointer: `this` is automatically available inside non-static member functions.

- Member Access: Allows access to the calling object's data members and methods.

- Self-Reference: Useful for operations involving comparisons or modifications based on the object's own state.

No comments:

Post a Comment