Trying to teach the class to preserve functions. For example, when writing a texture manager, specify the function that will load these textures; while the user can choose whatever he wants, and the manager will use it.
Found a similar question , on the basis of the information found, sketched an example. What I need does not work - I marked these places with comments with a question mark.
Here is an example:
#include<iostream> using namespace std; class TEST { public: //private: int (TEST::*UKAZ_ARG)(int a, int b); //Переменная для хранения выбранной функции int A_SUM(int a, int b) //Возможная функция 1 { return a+b; } int A_MNO(int a, int b) //Возможная функция 2 { return a*b; } public: TEST() //Конструктор { UKAZ_ARG = &TEST::A_MNO; //Функция по умолчанию } void ARG_REGIST( int (*func)(int,int) ) { // UKAZ_ARG = *func; //Не работает. Почему ? } int ROZRAH(int a,int b) //Функция, которая вызывает указатель на функцию (для красивого вызова) { //return TEST::((*UKAZ_ARG)(a,b)); //Не работает. Почему ? } }; int A_MNO_DIL(int a, int b) //Возможная функция 3 { return (a*b)/2; } int main() { TEST ob1; ob1.ARG_REGIST(A_MNO_DIL); //Не функционирует //cout<<ob1.UKAZ_ARG(5,5); cout<<(ob1.*(ob1.UKAZ_ARG))(5,10); //Не красиво и неудобно. Пытаюсь упаковать в `ROZRAH`, но в классе конструкция не работает cin.get(); return 0; }