131: Data Types: Function Pointers Part 2.

Take Up Code - Un pódcast de Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categorías:

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);

Visit the podcast's native language site