Home arrow C++ arrow Page 3 - Function Pointers, part 2
C++

Function Pointers, part 2


In the previous article, Jun Nakamura introduced you to the use of regular function pointers, but when you write C++ code, you will be interested in C++ class member function pointers too. It is time to look at how to declare pointers to the members of classes you write for your applications.

Author Info:
By: J. Nakamura
Rating: 4 stars4 stars4 stars4 stars4 stars / 11
May 31, 2005
TABLE OF CONTENTS:
  1. · Function Pointers, part 2
  2. · Defining and Using C Member Function Pointers
  3. · C Member Function Pointers and Polymorphism
  4. · The Member Function Pointer as Argument and Return Value

print this article
SEARCH DEVARTICLES

Function Pointers, part 2 - C Member Function Pointers and Polymorphism
(Page 3 of 4 )

How do member function pointers hold up in the face of Polymorphism? Will the function in a derived class be executed when we use a member function pointer containing the address of the baseclass function? Let me show you what I am getting at.

A typical way to make good use of polymorphism is to have a collection of base class pointers that are pointing to instantiated derived objects. To do this, you have to create the derived object and use the pointer to its baseclass:

class Base {
public:
virtual ~Base()=0;
virtual int foo() const=0;
};

class Deriv1 : public Base { public: int foo() { return 0; } };
class Deriv2 : public Base { public: int foo() { return 1; } };

// somewhere in your code
Base *ptr1 = new Deriv1;
int result = ptr1->foo(); // result will be 0

One of the great benefits of polymorphic classes is that you don’t have to be familiar with their implementation, only with their interface as it is declared in the baseclass. In the example above we know that there is a function called foo() but we don’t have to know what it exactly does. This can be a very useful concept for example in a game where there could be a list of game objects that all implement a run() function. The main game loop can call this run function on every game object, without having to know it is dealing with physics, A.I. or particle system objects. This also means you can add new game objects that implement different behaviors (and/or visuals) without having to make changes to the main code base.

So basically my question is: “When I use a member function pointer to Base::run() and use it on a Base* pointer that actually points to a derived game object… will the code in that game object be executed or will the code in the base object be executed?” A flipside question is whether it is possible to combine the member function pointer to Base::run() on a pointer (or reference) to a derived game object… thus not directly on Base itself. Let’s try this out with our Base and Deriv1 and Deriv2 example.

typedef int (Base::*BaseFooPtr)();
// ... later
BaseFooPtr fooptr = &Base::foo;
// ... test1 – try the BaseFooPtr on Base* to a Deriv1 object.
Base *baseptr = new Deriv1;
int result1 = (baseptr->*fooptr)();
// ... test2 – try the BaseFooPtr on a Deriv2 object.
Deriv2 myDeriv2;
Int result2 = (myDeriv2.*fooptr)();

Running this code you will find that member function pointers behave the same way you would expect normal function calls to behave in the face of polymorphism… answering both of my questions. The results are the same as when we wouldn’t have used a member function pointer but the regular functions calls instead. Thus result1 is 0 and result2 is 1!


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials