Javatpoint Logo
Javatpoint Logo

Pointer to an object in C++

In this article, we will discuss the pointer to an object in C++ with its examples.

What is a pointer?

In programming languages, a pointer is a variable that stores the memory address of another variable. In simple terms, a pointer "points" to a particular place in memory where a value is stored. Pointers are frequently employed for activities including dynamic memory allocation, effective data access, and building intricate data structures because they allow for indirect manipulation of variables.

In languages like C and C++, pointers provide programmers with direct memory access, giving them more control over memory management and data processing. Pointers can be used in object-oriented programming to refer to variables with fundamental data types, arrays, functions, or even actual objects.

Here's a basic definition of a pointer:

"A pointer is a variable that holds the memory address of another variable, allowing indirect access and manipulation of values."

Syntax:

The syntax for the pointer variable is given as:

In the above syntax, * indicates that the variable is a Pointer and will store the variable's address. We can use the pointer variable in the code to change the value of the main variable by depointing it.

Direct Memory Access:

Pointers give a mechanism to access and change memory-based data directly. It is especially helpful when working with huge datasets because it lets us save the cost of data copying. Take an array as an example. We can send a pointer to the array's first element to a function rather than the complete array, saving memory and boosting efficiency.

Memory Management:

When we use a language like Java or Python with automated memory management, the system manages memory allocation and deallocation. But in C and C++, memory management is explicitly our responsibility. In this process, pointers are essential. We can dynamically allocate memory using functions like malloc() or new, and free() or delete can deallocate it. As a result, memory usage can be more precisely controlled, memory leaks can be avoided, and resource usage can be improved.

What is a Pointer to an object?

A pointer to an object in C++ is a variable that contains an object's memory address. Pointers provide indirect access to and control over memory items. They are especially helpful when we need to dynamically allocate memory for objects, build linked lists or trees, or pass objects by reference to methods without making duplicates.

The behavior of an object pointer is identical to that of a variable pointer. But in this case, the object's address is kept instead of the variables. When a class object is formed in the main function, a pointer variable is declared similarly to the variable itself. Using a data type for the pointer is not recommended when generating a pointer to an object. Instead, we must make use of the object pointer's class name. The -> symbol must be used to call a class member function using a Pointer in the main function.

Syntax:

It has the following syntax:

Declaring a Pointer to an Object:

We declare a pointer to an object using the object's class name followed by an asterisk (*) and the pointer name.

Creating Objects and Pointers:

Objects are created using the new keyword, which dynamically allocates memory for the object. After that, the Pointer is assigned the memory address of the newly created object.

Accessing Object Members via Pointer:

We can use the arrow operator (->) to access members (variables and functions) of the object through the pointer.

Dereferencing a Pointer:

We "dereference" the pointer using the asterisk (*) operator to access the object itself (rather than its members).

Deleting Objects and Pointers:

When we're done with the dynamically allocated object, freeing the memory using the delete keyword is important. Failing to do so can result in memory leaks.

Null Pointers:

Pointers can also hold a special value, nullptr, which indicates that they are not pointing to a valid memory address.

Passing Pointers to Functions:

Pointers are often used to pass objects by reference to functions, allowing the F\function to modify the original object.

Pointer Arithmetic:

Pointer arithmetic applies to pointers to arrays but not necessarily to pointers to objects.

Program:

Let's take an example to understand the pointer to an object in C++.

Output:

Data: 42
Data: 42
Data: 99
Data: 0
Data: 10
Data: 20

Explanation:

Class Definition:

In this example, the MyClass class is defined with a member variable data and a member function display() that prints the data value.

Creating and Using Pointers:

MyClass *objPtr = new MyClass(42);: A pointer objPtr is created to point to a dynamically allocated MyClass object with data initialized to 42.

Objects ->display(): The object's display() function is called using the Pointer, showing the object's data.

MyClassobj = *objPtr;: A new object obj is created by dereferencing objPtr, effectively copying the content of the object it points to.

obj.display();: The display() function is called for the new object obj.

Modifying Objects using Pointers:

modifyObject(object, 99);: The function modifyObject() is called, which takes a pointer to an object and modifies its data member to 99.

objects ->display();: The modified object data is displayed using the Pointer.

Deleting Dynamically Allocated Objects:

Delete object: The dynamically allocated object is deleted, freeing the memory allocated with new.

An array of Pointers to Objects:

An array of pointers to MyClass objects is created with MyClass *objArray[3];. A loop creates three dynamically allocated MyClass objects with varying data and stores their pointers in the objArray.

Another loop displays the data of each object in the objArray. A final loop deletes the dynamically allocated objects in the array to free the memory.

Complexity Analysis:

Time Complexity:

  • The time complexity of creating and initializing objects is O(1) for each object created. Therefore, creating the objects in the code has a time complexity of O(1).
  • Accessing and modifying objects through pointers has a time complexity of O(1) since it involves directly accessing a member variable or calling a member function.
  • Creating the array of pointers, accessing objects in the array, and deleting objects from the array all take O(n) time complexity, where n is the number of objects in the array (in this case, 3).
  • Deleting the dynamically allocated objects in the array takes O(n) time complexity, where n is the number of objects in the array (in this case, 3).
  • The time complexity of the entire code is O(n), where n is the number of objects created and manipulated.

Space Complexity:

Creating Objects and Pointers: The space complexity for creating objects and pointers is O(1) for each object/pointer, so the space complexity for this part of the code is O(1).

An array of Pointers: The space complexity for creating an array of pointers is O(n), where n is the number of objects in the array.

Additional Objects: The space complexity for creating additional objects (such as obj inside the loop) is O(1) for each object created.

Therefore, the overall space complexity of the code is O(n), where n is the maximum number of objects present in the program at any given time (including the array of pointers and other objects).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA