Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!feeder.news-service.com!news.albasani.net!.POSTED!not-for-mail From: "Default User" Newsgroups: comp.std.c++ Subject: Pointers to member functions Date: Wed, 24 Aug 2011 17:38:11 -0700 (PDT) Organization: albasani.net Lines: 109 Sender: std-cpp-request@vandevoorde.com Approved: stephen.clamage@oracle.com Message-ID: <9bip7iFevdU1@mid.individual.net> X-Trace: news.albasani.net L8eA4LB0d6GXDENL+zndJ7nKuEAESjiC2Ae41fgskqPWox8bOsXQk6rEkCy00Xg4+lSH1PoapPtfwjol9QQh1w== NNTP-Posting-Date: Thu, 25 Aug 2011 00:38:13 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="ml3SQ6ps1uU5mHdHHJAWmGXUEDRaimCi0Jn1HZUyQGw27Qu5KRd/mjah+JYsNjJBhtlxb6dbO1aOhnFAse0RLIpynZrlTtLKUrypfjGeZ5FP4CpME/W3CBdmd4/PtzZz"; mail-complaints-to="abuse@albasani.net" X-Mailer: Perl5 Mail::Internet v2.05 X-Submission-Address: std-cpp-submit@vandevoorde.com Cancel-Lock: sha1:RwwiLfRk50A0dkrzX7rfeoMTUDw= X-Original-Date: Tue, 23 Aug 2011 17:51:29 -0500 Xref: x330-a1.tempe.blueboxinc.net comp.std.c++:248 This is a follow-up to a discussion from over in CLC++. Background. I'm working on a number of modules that all derive from a common base class. Part of the design that I came up with maps integer operation codes to pointers to member functions. After I did few, I noticed that I had identical functions (represented in the example below by "write" and "list"). So my thoughts were to try to push them down to the base class. This all seemed to "work", meaning that it built without issue and gave the results I was looking for. The fine folks over on the other group have pretty much convinced me that it's undefined behavior. I guess the big problem is that there isn't a round-trip on the function pointers, they're still a pointer to baseclass member function when called. I just wanted to run this past here to double-check that, and to see if there was perhaps something that could be done to tweek what I have and get a similar structure that was legal. Brian // This is example code only that gives the flavor of what I'm doing. // Please accept on faith that I really do need to do the mapping described. #include #include #define CALL_HANDLER(object,ptrToMember)((object).*(ptrToMember)) class tbase { public: typedef int (tbase::*HandlerPtr)(int); void write(int data, int code) { std::map::iterator it; it = handlers.find(code); if (it != handlers.end()) { // Found, call the function pointer CALL_HANDLER(*this, it->second)(data); } else // unhandled code { // error handling } } void list() { std::map::iterator it = handlers.begin(); std::cout<< "Supported Op codes:\n"; while (it != handlers.end()) { std::cout<< it->first<< "\n"; it++; } } protected: tbase() { } virtual ~tbase() { } std::map handlers; }; class test : public tbase { public: test() { handlers[0] = static_cast(&test::H0); handlers[4] = static_cast(&test::H4); } virtual ~test() { } virtual int H0(int data) { std::cout<< "test::H0: "<< data<< "\n"; return data; } int H4(int data) { std::cout<< "test::H4: "<< data<< "\n"; return data; } }; int main() { test t; t.list(); t.write(123, 0); t.write(456, 4); return 0; } -- [ comp.std.c++ is moderated. To submit articles, try posting with your ] [ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]