Error Cannot Create A Non-constant Pointer To Member Function
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow illegal operation on bound member function expression c++ the company Business Learn more about hiring developers or posting ads with us Stack Overflow c++ member function pointer Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of
C++ Error C2276
4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to best pass methods into methods of the same class up vote 6 down vote favorite 3
Use '&' To Create A Pointer To Member
I have this C++ class that one big complicated method compute that I would like to feed with a "compute kernel", a method of the same class. I figure I would do something along the lines of class test { int classVar_ = 42; int compute_add(int a, int b) { compute(int a, int b, this->add_()) } int compute_mult(int a, int b) { compute(int a, int b, this->mult_()) } int compute_(int a, int b, std::invoke no matching overloaded function found "pass in add or multiply as f()") { int c=0; // Some complex loops { c += f(a,b) // } return c; } int add_(int a, int b){a+b+classVar_;} int multiply_(int a, int b){a*b+classVar_;} ... } but I'm not sure how I would pass in add or multiply. An alternative to this approach would be to pass in an ENUM of some sort to specify add() or multiply(), but I wanted to avoid a switch or if inside the loops. What's best practice here? c++ member-function-pointers share|improve this question edited Jun 29 '12 at 10:44 asked Jun 29 '12 at 10:36 Nico Schlömer 2,87622442 1 You have a member-function-pointers tag. Isn't that a good place to start? –chris Jun 29 '12 at 10:38 @chris i guess he's confused about the syntax? –RedX Jun 29 '12 at 10:42 1 If the methods you want to pass are as in your sample, plain (static) functions & ordinary function pointers are enough. –Mat Jun 29 '12 at 10:43 @RedX, Not really a problem with Google, seeing as how it is a pretty common topic for source code. –chris Jun 29 '12 at 10:45 add a comment| 3 Answers 3 active oldest votes up vote 5 down vote accepted As you suspected, passing a
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn
Expression Preceding Parentheses Of Apparent Call Must Have (pointer-to-) Function Type
more about Stack Overflow the company Business Learn more about hiring developers or std::function member function posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community non-standard syntax; use '&' to create a pointer to member Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Create a non-constant pointer to member function for SDL http://stackoverflow.com/questions/11260260/how-to-best-pass-methods-into-methods-of-the-same-class event filtering up vote 1 down vote favorite I'm playing with SDL, and I am trying to supply a function pointer to an event filter. This works fine if I make the function a static member of ObjectWithState, but I'd like to have the callback function alter the state of the object. I was hoping to do this perhaps using a functor, but I can't quite work it http://stackoverflow.com/questions/19938531/create-a-non-constant-pointer-to-member-function-for-sdl-event-filtering out. Is there any C++11 trickery that I can use to make this work? class ObjectWithState { int someState; public: int operator()(void* userData, SDL_Event *event) { return ++someState; } }; int main() { //boilerplate ObjectWithState obj; SDL_EventFilter f = &(obj.operator()); //ERROR -> cannot create non-constant pointer to member function SDL_SetEventFilter( f, nullptr ); } c++ c++11 sdl function-pointers member-function-pointers share|improve this question edited Nov 13 '13 at 9:43 asked Nov 12 '13 at 19:48 learnvst 7,35173573 Unrelated: your operator() doesn't actually return an int as declared. –Casey Nov 12 '13 at 22:32 YEah, sorry, oversimplification of situation –learnvst Nov 12 '13 at 22:42 add a comment| 2 Answers 2 active oldest votes up vote 4 down vote accepted Use the userdata parameter to point to your object, and dispatch through a static method to the non-static method: class ObjectWithState { int someState; public: int operator()(SDL_Event *event) { ++someState } static int dispatch(void* userdata, SDL_Event* event) { return static_cast
a useful yet poorly understood language feature, useful to cache the outcome of a decision or to enable a different sort of polymorphism. Mike Crawford Consulting Software Engineer mike@soggywizards.com Copyright © 2002, 2012, 2016 Michael D. Crawford. This http://soggywizards.com/tips/code/c++/member-pointers.html work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. Abstract Pointers to Member https://isocpp.org/wiki/faq/pointers-to-members Functions are one of C++'s more rarely used features, and are often not well understood even by experienced developers. This is understandable, as their syntax is necessarily rather clumsy and obscure. While they do not have wide applicability, sometimes member function pointers are useful to solve certain problems, and when they do apply they are often the member function perfect choice, both for improved performance and to make the code sensible. They work very well to cache the result of a frequently made decision, and to implement a different sort of polymorphism. I discuss what member function pointers are, how to declare and use them, and give some examples of problems that they solve very well. Contents Abstract Introduction Member Function Pointers Are Not Just Simple Addresses Caching the Outcome pointer to member of a Decision The Performance of Member Function Pointers Details About Using Member Function Pointers A Different Sort of Polymorphism Introduction I don't have any hard numbers on how frequently member function pointers are used. While I do see others mention them sometimes in Usenet and mailing list posts, I have yet to find someone else use one in code I have worked with, so my impression is that they are not commonly applied. Exceptional C++ Style 40 New Engineering Puzzles, Programming Problems, and Solutions (C++ in Depth Series) by Herb Sutter [ Buy] Member function pointers are important because they provide an efficient way to cache the outcome of a decision over which member function to call. They can save time, and in some cases, provide a design alternative that avoids the need to implement such decision caching through memory allocation. I will return to this further on. Member function pointers allow one to call one of several of an object's member functions indirectly. Each of the functions whose "address" is stored must share the same signature. I put "address" in quotes because the information stored in a member function pointer is not simply the memory address of the start of the member function's code; conceptually it is an offset in
Navigation FAQ Home FAQ RSS Feed FAQ Help Search this Wiki Go to Page Upcoming Events Fall ISO C++ standards meeting Nov 7-12, Issaquah, WA, USA Meeting C++ Nov 18-19, Berlin, Germany ACCU 2017 Apr 26-29, Bristol, UK Tweets by @isocpp Wiki Home > pointers to members View pointers to members Save to: Instapaper Pocket Readability Pointers to Member Functions Is the type of "pointer-to-member-function" different from "pointer-to-function"? Yep. Consider the following function: int f(char a, float b); The type of this function is different depending on whether it is an ordinary function or a non-static member function of some class: Its type is "int (*)(char,float)" if an ordinary function Its type is "int (Fred::*)(char,float)" if a non-static member function of class Fred Note: if it's a static member function of class Fred, its type is the same as if it were an ordinary function: "int (*)(char,float)". How do I pass a pointer-to-member-function to a signal handler, X event callback, system call that starts a thread/task, etc? Don't. Because a member function is meaningless without an object to invoke it on, you can't do this directly (if The X Window System was rewritten in C++, it would probably pass references to objects around, not just pointers to functions; naturally the objects would embody the required function and probably a whole lot more). As a patch for existing software, use a top-level (non-member) function as a wrapper which takes an object obtained through some other technique. Depending on the routine you're calling, this "other technique" might be trivial or might require a little work on your part. The system call that starts a thread, for example, might require you to pass a function pointer along with a void*, so you can pass the object pointer in the void*. Many real-time operating systems do something similar for the function that starts a new task. Worst case you could store the object pointer in a global variable; this might be required for Unix signal handlers (but globals are, in general, undesired). In any case, the top-level function would call the desired member function on the object. Here's an example of the worst case (using a global). Suppose you want to call Fred: