Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | "Default User"<defaultuserbr@yahoo.com> |
|---|---|
| Newsgroups | comp.std.c++ |
| Subject | Pointers to member functions |
| Date | 2011-08-24 17:38 -0700 |
| Organization | albasani.net |
| Message-ID | <9bip7iFevdU1@mid.individual.net> (permalink) |
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<iostream>
#include<map>
#define CALL_HANDLER(object,ptrToMember)((object).*(ptrToMember))
class tbase
{
public:
typedef int (tbase::*HandlerPtr)(int);
void write(int data, int code)
{
std::map<int, HandlerPtr>::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<int, HandlerPtr>::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<int, HandlerPtr> handlers;
};
class test : public tbase
{
public:
test()
{
handlers[0] = static_cast<HandlerPtr>(&test::H0);
handlers[4] = static_cast<HandlerPtr>(&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 ]
Back to comp.std.c++ | Previous | Next — Next in thread | Find similar
Pointers to member functions "Default User"<defaultuserbr@yahoo.com> - 2011-08-24 17:38 -0700
Re: Pointers to member functions Daniel Krügler<daniel.kruegler@googlemail.com> - 2011-08-25 20:51 -0600
Re: Pointers to member functions "Default User" <defaultuserbr@yahoo.com> - 2011-08-26 11:19 -0700
csiph-web