I need to store pointers to class methods, and then call these methods.

Here is a refined example:

class Test { int testmethod(int value) { return value; } int (Test::*func)(int) = &Test::testmethod; }; int main() { Test test; Test* testpointer = &test; (testpointer->*func)(3); return 0; } 

It seems that the syntax is exactly the same as that one, but the compiler swears:

 maip.cpp: В функции «int main()»: maip.cpp:35:17: ошибка: нет декларации «func» в этой области видимости (testpointer->*func)(3); ^ 

I am using gcc 5.3.0

    2 answers 2

    First, in order to reach a member of a class like this, it must be public, so we add a public section, or change the class to struct . Secondly, because func is a member of a class, not a free object, then we cannot access it outside the class object, so the correct entry, in this case, will look like this:

     (testpointer->*test.func)(3); 

    Or so:

     (test.*test.func)(3); 
    • Without denying that if you need to turn to a member of a class, then yes, exactly, I have a question: why ?! in that sense, at least, why should we keep obviously identical pointers in each object? then it's better to make it static ... - Harry
    • @Harry, my business is to answer clearly asked questions, and not think through the author's code. This is not a grateful thing - when the author grows up to an understanding of how to write correctly, and how not, then he will stop writing curved code. Explanations in the network, it seems to me, fly into one ear and fly out of the other. - ixSci
    • Well, as Sheckley argued, in order to ask a question, you need to know half of the answer ... So the question is not at all about what is necessary :) ([Thoughtfully scratching the back of his head] is interesting, asking you the question “why”, which I really had to ask a question? :) - Harry
    • @Harry, in my opinion, the main thing here is that the user understands the syntax, understands why his code does not work, plays with it. Where he will eventually place the pointer, in my opinion, is deeply secondary. - ixSci
    • @ixSci I forgot about public, yes, in my code this kitchen is all inside the object, so there is visibility. And I don't seem to be appealing outside the object, because in the expression (testpointer -> * func) there is a pointer to a specific object, isn't it? The syntax used in my example is seen in several places, for example, here rsdn.ru/article/cpp/fastdelegate.xml#EGD - user206565

    Try to make

     int (Test::*func)(int) = &Test::testmethod; 

    from the class...

    • But I will need to store pointers in a vector inside an object of this class. - user206565
    • You know, something tells me that you have not very thoughtful design. You want to store in each object an additional function pointer, the same for all objects. WHAT FOR? When calling (testpointer->*sest.func)(3); (note that testpointer points to one object, sest is another , which is needed just to indicate which object to take a pointer to a function ... Why is it inefficient in terms of memory and a difficult solution? - Harry
    • I tried to remove all unnecessary from the example, so it came out very synthetically. In general, the idea is to store pointers to the methods of the structures in the vector. Then, if the fields of any of them meet the conditions, the method is called from the saved pointer. In my opinion, this is a more elegant solution than a woolly switch or a footcloth from if-Elsa. If you have a more elegant alternative, you would love to hear it. - user206565
    • Honestly, I didn’t quite understand your problem. Is there a vector of structures, if the structure is satisfied with some condition, call its method? What's the point of storing pointers? Or, if there are a lot of variants of methods, then all the same, the test will be a “hairy switch”, only inside the test? But usually, when you come across a footcloth, as you say, inheritance and virtual functions immediately come to mind. But once again - I do not understand very clearly what exactly you need. - Harry