CPP Pointers to Class Methods
Pointers to Class Methods
-
For a pointer to a method we use the same syntax as with a pointer to function,
but include the class name ending with the scoping operator (::).
double (Shape::*pointerFunction) (double, double);
double (Shape::*pointerFunction) (double, double); pointerFunction=&Shape::getArea;
-
To execute the method using the pointer to method .
double (Shape::*pointerFunction) (double, double); double area=(shapeInstance.*pointerFunction)(5.0, 3.0); // and if the shape object is on the free store double area=(shapePointer->*pointerFunction)(5.0, 3.0);
- The pointer to methods is in a way polymorphic and we normally solve this though use of virtual methods in a class inheritance is-a relationship.
- The pointer to methods can also be combined with polymorphism.
-
A pointer to a class method can be created inside a class or outside a class.
Here is an example how you can do that:
#include <iostream> using namespace std; class Shape { protected: int width, height; int (Shape:: * pointerArea) (); void (Shape:: * pointerSetValues)(int, int); public: Shape () { pointerArea=&Shape::area; pointerSetValues=&Shape::set_values; } virtual int area ()=0; void set_values (int a, int b) { width=a; height=b;} int getPointerArea(){ return (this->*pointerArea)(); } void setPointerValues(int width, int height){ (this->*pointerSetValues)(width, height); } }; class Rectangle: public Shape { public: int area () { return (width * height); } }; class Triangle: public Shape { public: int area () { return (width * height / 2); } }; int main () { Rectangle rect; Triangle trgl; int width=6; int height=5; rect.set_values (width,height); trgl.set_values (width,height); cout << "For width=" << width << " and height=" << height << " the Rectangle area=" << rect.area() << endl; cout << "For width=" << width << " and height=" << height << " the Triangle area=" << trgl.area() << endl; cout << "\nUsing pointers to methods:\n"; // Creates a pointer to method Area() (which is virual) // in the Shape class int (Shape:: * pArea)()= pArea=&Shape::area; void (Shape:: * pSetValues)(int, int)=&Shape::set_values; // Set values using pointer to methods width=4; height=7; (rect.*pSetValues)(width,height); (trgl.*pSetValues)(width,height); // Get Area using pointer to methods // As area() is virtual the area() method for the right object is executed int rectArea=(rect.*pArea)(); int trglArea=(trgl.*pArea)(); cout << "For width=" << width << " and height=" << height << " the Rectangle area=" << rectArea << endl; cout << "For width=" << width << " and height=" << height << " the Triangle area=" << trglArea << endl; cout << "\nUsing methods that use pointer to method in the Shape class:\n"; // Set values using methods that use pointer to methods in the class width=5; height=8; rect.setPointerValues(width,height); trgl.setPointerValues(width,height); // Get Area using methods that use pointer to methods in the class // As area() is virtual the area() method for the right object is executed rectArea=rect.getPointerArea(); trglArea=trgl.getPointerArea(); cout << "For width=" << width << " and height=" << height << " the Rectangle area=" << rectArea << endl; cout << "For width=" << width << " and height=" << height << " the Triangle area=" << trglArea << endl; return 0; }
When we run this application, the result will be:For width=6 and height=5 the Rectangle area=30 For width=6 and height=5 the Triangle area=15 Using pointers to methods: For width=4 and height=7 the Rectangle area=28 For width=4 and height=7 the Triangle area=14 Using methods that use pointer to method in the Shape class: For width=5 and height=8 the Rectangle area=40 For width=5 and height=8 the Triangle area=20
You can download this example here (needed tools can be found in the right menu on this page).
© 2010 by Finnesand Data.
All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the
correctness in the pages or source codes.
The risk of using this web-site pages or any program
codes from this website is entirely at the individual user.