I have a class within which an object of another class is declared (composition). The first contains a method, a pointer to which you want to pass to the second class constructor. It turned out. How then to call a method on the pointer? In reality, several objects of different classes are declared inside the class. There is one function to use in some of the inner classes. Therefore, trying to figure out how to do it. I try to transfer the pointer to it.

header file

class CharScreen; // класс внутри которого будет объявлен объект Loader class Loader { private: void (CharScreen::* Conv)(uint8_t index); // указатель на метод public: Loader(void (CharScreen::* Convertor)(uint8_t index)); void CalculateMenuSize(); // метод внутри которого нужно сделать // вызов, переданного в конструктор // метода, по указателю }; class CharScreen { private: Loader Loader; // объявляю объект Loader public: CharScreen(); void ConvertVar(uint8_t index); // тот самый метод, который нужно // передать в конструктор Loader }; 

cpp

 Loader::Loader(void (CharScreen::* Convertor)(uint8_t index)): { Conv = Convertor; // присваиваю объявленному в заголовке указателю, // преданный в конструктор параметр-указатель на метод } void Loader::CalculateMenuSize() { (*Conv)(1); // вот здесь возникает проблема "operand of "*" must be a // pointer" } CharScreen::CharScreen(): Loader(&CharScreen::ConvertVar) { Loader.CalculateMenuSize(); } void CharScreen::ConvertVar(uint8_t index) { } 

    2 answers 2

    Class member declaration

      Loader Loader; 

    is unacceptable. Or

      class Loader Loader; 

    or make it so that the field name does not match the class name. (In fact, the standard in this matter is somehow ambiguous ...)


    Well, and for which object of the CharScreen class CharScreen you going to call this method? No object - nothing to call.

    My guessing on the coffee grounds tells me that you want to call this method from an instance of the Loader class specifically for the CharScreen object that contains this instance of the Loader class. In C ++ there are no ready-made tools for moving from the contents of an object to the containing object. Hacks can help here, such as the famous container_of

     #define container_of(ptr, type, member)\ ((type *) ((char *) ptr - offsetof(type, member))); void Loader::CalculateMenuSize() { CharScreen *owner = container_of(this, CharScreen, Loader); (owner->*Conv)(1); } 

    but here you have to fight with access rights.

    It would be better to simply use std::function<> to store the "pointer" to the method, and form the correct instance of such std::function<> in the constructor code (using std::bind or std::bind else).

      In the CalculateMenuSize argument, you need to pass a reference to the CharScreen object if the CharScreen objects CharScreen many instances so that there are no misunderstandings. Or if it is one, then make the ConvertVar method static , then you can not use a specific object.

       void Loader::CalculateMenuSize(CharScreen & obj) { (obj.*Conv)(1); } 

      or

       class Loader { void (*Stat)(uint8_t);}; class CharScreen { static void ConvertVar(uint8_t index); }; void Loader::CalculateMenuSize() { Stat(1); }