Pointer with Classes

Using Pointers with Classes

Creating and Accessing Class Objects

You can use pointers to create and access objects dynamically. This is particularly useful for managing the lifetime of objects and for creating arrays of objects.

Example
#include <iostream>
using namespace std;

class MyClass {
public:
    void display() {
        cout << "Hello from MyClass!" << endl;
    }
};

int main() {
    MyClass obj; // Stack allocation
    obj.display();

    MyClass *ptr = new MyClass; // Dynamic allocation
    ptr->display();

    delete ptr; // Free dynamically allocated memory
    return 0;
}

In this example:

  • obj is an instance of MyClass created on the stack.
  • ptr is a pointer to an instance of MyClass created dynamically on the heap using the new keyword.
  • The arrow operator (->) is used to access members of the class through the pointer.

Accessing Members Using Pointers

When using pointers to access class members, you use the arrow operator (->) instead of the dot operator (.).

Example
#include <iostream>
using namespace std;

class MyClass {
public:
    int value;
    void display() {
        cout << "Value: " << value << endl;
    }
};

int main() {
    MyClass obj;
    obj.value = 10;
    obj.display();

    MyClass *ptr = new MyClass;
    ptr->value = 20;
    ptr->display();

    delete ptr;
    return 0;
}

In this example:

  • obj.value and obj.display() are used to access members of obj.
  • ptr->value and ptr->display() are used to access members of the dynamically allocated object through the pointer ptr.

Dynamic Memory Management with Classes

Dynamic memory management is crucial when working with classes and pointers. This involves using new and delete to allocate and deallocate memory for objects dynamically.

Example: Array of Objects

#include <iostream>
using namespace std;

class MyClass {
public:
    int value;
    void display() {
        cout << "Value: " << value << endl;
    }
};

int main() {
    int size = 3;
    MyClass *arr = new MyClass[size]; // Dynamic array of objects

    for (int i = 0; i < size; ++i) {
        arr[i].value = i * 10;
    }

    for (int i = 0; i < size; ++i) {
        arr[i].display();
    }

    delete[] arr; // Free dynamically allocated array
    return 0;
}

In this example:

  • A dynamic array of MyClass objects is created using new MyClass[size].
  • The array is accessed using the subscript operator ([]).
  • The array is deallocated using delete[].

Passing Objects to Functions Using Pointers

Passing objects to functions using pointers can be more efficient than passing by value, especially for large objects. It also allows the function to modify the original object.

Example

#include <iostream>
using namespace std;

class MyClass {
public:
    int value;
    void display() {
        cout << "Value: " << value << endl;
    }
};

void modifyObject(MyClass *obj) {
    obj->value = 100;
}

int main() {
    MyClass obj;
    obj.value = 10;
    obj.display();

    modifyObject(&obj); // Passing object by pointer
    obj.display();

    return 0;
}

In this example:

  • modifyObject takes a pointer to MyClass as an argument and modifies the object’s value.
  • The original object obj is passed to the function using the address-of operator (&).

Smart Pointers with Classes

Smart pointers, available in the C++ Standard Library, manage the lifetime of dynamically allocated objects automatically. They are part of the <memory> header and include std::unique_ptr, std::shared_ptr, and std::weak_ptr.

Example: Using std::unique_ptr

#include <iostream>
#include <memory> // For std::unique_ptr
using namespace std;

class MyClass {
public:
    MyClass() {
        cout << "Constructor called" << endl;
    }
    ~MyClass() {
        cout << "Destructor called" << endl;
    }
    void display() {
        cout << "Hello from MyClass!" << endl;
    }
};

int main() {
    unique_ptr<MyClass> ptr = make_unique<MyClass>(); // Creating a unique_ptr
    ptr->display();

    // No need to delete the object manually
    return 0;
}

In this example:

  • std::unique_ptr is used to manage the lifetime of the MyClass object.
  • The object is automatically destroyed when the unique_ptr goes out of scope.