When to use a pointer rather than the object itself in C++?

The instance of a class in C++ is called an object. To allocate memory for the class we need to create an object for that class. We can access the members of the class using the dot (.) operator.

class_name object_name;

The name of the class will replace the class_name we want to create an object for, and object_name will be any name we want to choose for the object.

If we want to access the member function print() from the class using an object, we can do this by using object_name.print().

Why use objects?

  • Objects provide faster access. We don’t need to dereference it.
  • We don’t need to explicitly delete the object after using it (no memory leaks).

Pointer to objects

Objects can be accessed in two ways:

  • Direct access
  • Using a pointer to that object. To declare an object, we follow the same structure as for declaring pointers.
class_name *object_name = new class_name();

Here, the asterisk (*) is used to declare the pointer and the new keyword is used to allocate memory on the heap for the object.

We use the member access operator (->) to access the member using a pointer to the object.

Why use pointer objects?

  • The size isn’t limited because it’s not created on the stack.
  • The scope of the object pointer isn’t limited to the method where it’s created. We need to explicitly delete it after use to avoid memory leaks.
  • There’s no need to create a copy when we try to return objects from a function.

Example

#include <iostream>
using namespace std;
class myClass{
public:
void demoObject(){
cout<<"This function is accessed using an object."<<endl;
}
void demoPointer_to_object(){
cout<<"This function is accessed using a pointer to the object."<<endl;
}
};
int main() {
myClass obj;
myClass *obj1;
obj.demoObject();
obj1 = &obj;
obj1->demoPointer_to_object();
return 0;
}

Explanation

  • Line 3: We create a class, myClass.
  • Line 5: We create a function that prints how it’s called.
  • Line 15: This is an object for class myClass.
  • Line 16: This is a pointer of the class *obj1.
  • Line 18: We assign the address of the object to the pointer using the & operator. Now when the object is updated, the pointer is also updated.
  • Line 19: We use the object pointer obj1 to access the member function of the class.

When to use a pointer over an object

  • Using an object itself is an expensive operation; for example, if we need to return an object from a function, we’ll need to create copies, which will make it an expensive operation. We can avoid this by using a pointer to an object as multiple pointers can point to the same object.
  • In case where we want to point an object to another object, we can use a pointer instead of just creating a copy. We can understand this by a simple scenario: Imagine ordering something from a store and specifying the delivery address. Instead of sending an identical item to the store, we provide them with the delivery address (a pointer) so they can deliver the item to our desired location. This approach avoids unnecessary duplication and ensures that the correct item reaches the intended destination.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved