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; } 
  • don't ask a few questions in one - Abyx
  • But is this function that you are trying to establish whether it should have access to the properties of an instance of this class? If yes, then it is declared correctly as a member of the class, and besides the two int, it also receives this, but then you cannot assign a function to it from outside this class, since it does not and cannot have any this. If access is not needed, then you do not need to declare it as a member of the class and then you can use as parameters any external functions or static members of the class (in which this is also not) - Mike
  • I need access from both inside and outside, access from outside. Assigning a function from outside to a higher priority. - Yuriy Pysanka

3 answers 3

In the first question, you have UKAZ_ARG - a pointer to a member function, and you are trying to pass a free function to it. These are completely different kinds of functions. After all, in fact, some A_MNO gets three arguments - two int , and one more - the TEST object for which it is called. And A_MNO_DIL is just two int 's. In order to avoid an incorrect call, such an assignment is impossible ...

In the second, you have a pointer to a member function, and you actually call it as static, without specifying the desired object for which you want to call it (see above for the third call argument). To earn, write

 return (this->*UKAZ_ARG)(a,b); 
  • With the second everything turned out. And the first to make as. I saw in the glut library the registration function of the keyboard mouse, etc. the library somehow saves the function that I pass to it and calls as an event. - Yuriy Pysanka
  • Look - because you have in your functions A_MNO and A_SUM , the class object itself is not used anywhere. Well, make them static , and declare int (*UKAZ_ARG)(int a, int b); . By the way, it can also be made static. - Harry
  • Thank you very much, everything worked - Yuri Pysanka

Vso earned. Here is the result

 #include<iostream> using namespace std; class TEST { public: //private: int (*UKAZ_ARG)(int a, int b); //Переменная для хранения выбраной функцыи static int A_SUM(int a, int b) //Возможная фушкция 1 { return a+b; } static int A_MNO(int a, int b) //Возможная фушкция 2 { return a*b; } public: TEST() //Конструктор { UKAZ_ARG = &TEST::A_MNO; //Функия по умолчанию } void ARG_REGIST(static int (*func)(int a,int b) ) { // UKAZ_ARG = *func; //Не работает Почему ? UKAZ_ARG = *func; } int ROZRAH(int a,int b) //Функция которая вызивает указать функцыю (Для красивого вызова) { //return TEST::((*UKAZ_ARG)(a,b)); //Не работает почему ? return (this->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.ROZRAH(5,10); //Не красивр и не удобно пытаюсь упаковать в ROZRAH но в классе конструкция не работает cin.get(); return 0; } 

    More use std :: function

     class TEST { std::function<int(int, int)> UKAZ_ARG; public: TEST() { UKAZ_ARG = [this](int a, int b) { A_MNO(a, b); } // <- тут создали лямбда функцию для вызова A_MNO } void ARG_REGIST(cons std::function<int(int, int)>& func) { UKAZ_ARG = func; } int ROZRAH(int a,int b) { return UKAZ_ARG(a, b); } } void main() { TEST test; std::cout << test.ROZRAH(1, 2); // print "2" -> A_MNO(1, 2) == 2 test.ARG_REGIST([&test](int a, int b) { return test.A_SUM(1,2); }); std::cout << test.ROZRAH(1, 2); // print "3" test.ARG_REGIST([](int a, int b) { return a + b + 1; }); std::cout << test.ROZRAH(1, 2); // print "4" -> a=1 + b=2 + 1 == 4 } 
    • Then at least TEST() { UKAZ_ARG = [this](int a, int b) { return this->A_MNO(a, b); }; // <- тут создали лямбда функцию для вызова A_MNO } TEST() { UKAZ_ARG = [this](int a, int b) { return this->A_MNO(a, b); }; // <- тут создали лямбда функцию для вызова A_MNO } TEST() { UKAZ_ARG = [this](int a, int b) { return this->A_MNO(a, b); }; // <- тут создали лямбда функцию для вызова A_MNO } - Harry
    • The example does not work (a lot of errors) - Yuri Pysanka
    • @Harry, this is the taste of whether or not to write this, I am for not writing. - ffk
    • @ffk Well ... in general, yes ... But as for me - so much more clear. - Harry