C++ Pointers to Classes
A pointer to a C++ class is similar to a pointer to a structure; to access the 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 an ordinary pointer, a pointer to a class can be used to access the object's member variables and member functions.
Declaring and Initializing a Pointer to a Class
Example
#include
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 the pointer
std
::
cout
<<
"Data via pointer: "
<<
ptr
data
<<
std
::
endl
;
// Call member functions through the pointer
ptr
display
(
)
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
Data via pointer: 42
Data: 42
Dynamic Memory Allocation
A pointer to a class can also be used for dynamic memory allocation to create objects of the class:
Example
#include
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 member functions through the pointer
ptr
display
(
)
;
// Free the dynamically allocated memory
delete
ptr
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
Data: 42
Pointer to a Class as a Function Parameter
A pointer to a class can be passed as a function parameter:
Example
#include
class
MyClass
{
public
:
int
data
;
void
display
(
)
{
std
::
cout
<<
"Data: "
<<
data
<<
std
::
endl
;
}
}
;
// Function accepts a pointer to a class as a parameter
void
processObject
(
MyClass
ptr
)
{
ptr
display
(
)
;
}
int
main
(
)
{
MyClass obj
;
obj.
data
=
42
;
// Pass a pointer to the class to the function
processObject
(
&
obj
)
;
return
0
;
}
Example
The example below helps to better understand the concept of pointers to classes:
Example
#include
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 to use 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 to use the member access operator to access members
cout
<<
"Volume of Box2: "
<<
ptrBox
Volume
(
)
<<
endl
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
Constructor called.
Constructor called.
Volume of Box1: 5.94
Volume of Box2: 102
C++ Classes & Objects