C++ Pointer to a Class
A pointer to a C++ class is similar to a pointer to a structure. To access members of a pointer to a class, you need to use the member access operator->, just like accessing a pointer to a structure. Like all pointers, you must initialize the pointer before using it.
In C++, a pointer to a class points to an object of a class. Similar to ordinary pointers, a pointer to a class can be used to access the member variables and member functions of the object.
Declaring and initializing pointers to classes
Example
class MyClass {
public:
int data;
void display() {
std::cout << "Data: " << data << std::endl;
}
};
int main() {
// Create a class object
MyClass obj;
obj.data = 42;
// Declare and initialize a pointer to the class
MyClass *ptr = &obj;
// Access member variables through a pointer
std::cout << "Data via pointer: " << ptr->data << std::endl;
// Call a member function through a pointer
ptr->display();
return 0;
}
When the above code is compiled and executed, it produces the following results:
Data via pointer: 42 Data: 42
Dynamic memory allocation
A pointer to a class can also be used to dynamically allocate memory to create an object of the class:
Example
class MyClass {
public:
int data;
void display() {
std::cout << "Data: " << data << std::endl;
}
};
int main() {
// Dynamically allocate memory to create a class object
MyClass *ptr = new MyClass;
ptr->data = 42;
// Call a member function through a pointer
ptr->display();
// Release dynamically allocated memory
delete ptr;
return 0;
}
When the above code is compiled and executed, it produces the following results:
Data: 42
Pointers to classes as function parameters
A pointer to a class can be passed as a function parameter:
Example
class MyClass {
public:
int data;
void display() {
std::cout << "Data: " << data << std::endl;
}
};
// The function accepts a pointer to the class as a parameter
void processObject(MyClass *ptr) {
ptr->display();
}
int main() {
MyClass obj;
obj.data = 42;
// Pass the pointer to the class to the function
processObject(&obj);
return 0;
}
Example
The following example helps to better understand the concept of pointers to classes:
Example
using namespace std;
class Box
{
public:
// Constructor definition
Box(double l=2.0, double b=2.0, double h=2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
}
double Volume()
{
return length * breadth * height;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main(void)
{
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
Box *ptrBox; // Declare pointer to a class.
// Save the address of the first object
ptrBox = &Box1;
// Now try using the member access operator to access members
cout << "Volume of Box1: " << ptrBox->Volume() << endl;
// Save the address of the second object
ptrBox = &Box2;
// Now try using the member access operator to access members
cout << "Volume of Box2: " << ptrBox->Volume() << endl;
return 0;
}
When the above code is compiled and executed, it produces the following results:
Constructor called. Constructor called. Volume of Box1: 5.94 Volume of Box2: 102other extensions