Search

Home > Take Up Code > 131: Data Types: Function Pointers Part 2.
Podcast: Take Up Code
Episode:

131: Data Types: Function Pointers Part 2.

Category: Technology
Duration: 00:13:48
Publish Date: 2016-06-27 00:16:15
Description:

You may not always need to work with this data type but that doesn’t mean you can ignore it. If you don’t at least know the basics, then you’ll get lost in code that uses function pointers.

You can listen to the episode for the full description. I’ll use this space to show you how to declare and work with a class member function pointer.

// Class method declaration.
class Number
{
public:
	Number ()
	: mValue(0)
	{ }

	virtual int addValue (int value);

private:
	int mValue;
};

// Class method pointer type declaration compatible with the addValue method.
using AddPointer = int (Number::*)(int);

// Class method pointer variable declaration and assignment.
AddPointer functionPtr = &Number::addValue;

// Declaring an instance of the Number class and a pointer to that instance.
Number numberInstance;
Number * numberInstancePointer = &numberInstance;

// Calling the addValue method directly and then through the pointer.
int result = numberInstance.addValue(5);
result = numberInstancePointer->addValue(5);

// Calling the addValue method using the function pointer through both an instance and a pointer to an instance.
result = numberInstance.*functionPtr(5);
result = numberInstancePointer->*functionPtr(5);
Total Play: 0

Some more Podcasts by Take Up Code

300+ Episodes