Pointers to Objects:
The members of a class can be accessed through the pointer to the class. The arrow (–>) symbol is used to access them. This symbol is also known as member access operator. It is denoted by a hyphen (–) and a greater than sign (>). The general syntax to access a member of a class through its pointer is:
p –> class member
Where
[P] : is the pointer to the object.
[– >] : it is the member access operator. It is used to access the members of the object through the pointer.
[class member] : it is the member of the object.
If a pointer object is defined to point to a class, then it can also point to any of its subclass. However, even if the pointer points to an object of its derived class, it remains of base class type. A program may have a base class and several derived classes. When a pointer is declared to point to the base class, it can also point to the classes derived from this base class.
When a base class pointer is used to point to an object of a derived class, the pointer type remains unchanged; its type remains of the base class. When this pointer is used to access a member of a class with the member access operator (–>), the pointer type determines which actual function will be called. It always accesses the member function of the class with which its type matches. Thus it will execute the member function of the base class. This is the default method of execution of a member function through a pointer.
comment closed