Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pointers to member functions

Reply
Thread Tools

Pointers to member functions

 
 
Protoman
Guest
Posts: n/a
 
      07-01-2006
Why doesn't this code work; it should:

class Obj
{
public:
void a(){cout << "A!" << endl;}
void b(){cout << "B!" << endl;}
void c(){cout << "C!" << endl;}
};

int main()
{
void (Obj::*pFn)();
Obj obj;
pFn=&Obj::a;
obj->*pFn();
pFn=&Obj::b;
obj->*pFn();
pFn=&Obj::c;
obj->*pFn();
system("PAUSE");
return 0;
}

Help? Thanks!!!!!

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-01-2006
* Protoman:
> Why doesn't this code work; it should:


Protoman, you miserable old troll, /when/ are you going to read the FAQ
item on how to post?

Tell what you expect, and what actually happens.

Be precise, include the actual error message(s) by copying and pasting.


> class Obj
> {
> public:
> void a(){cout << "A!" << endl;}
> void b(){cout << "B!" << endl;}
> void c(){cout << "C!" << endl;}
> };


OK, except for lack of indentation.


> int main()
> {
> void (Obj::*pFn)();
> Obj obj;
> pFn=&Obj::a;
> obj->*pFn();


'obj' is not a pointer, so '->' is not applicable ('.' is).

Also, you need parentheses.


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Murali Krishna
Guest
Posts: n/a
 
      07-01-2006
> * Protoman:
> > void (Obj::*pFn)();
> > Obj obj;
> > pFn=&Obj::a;
> > obj->*pFn();


Alf P. Steinbach wrote:
> 'obj' is not a pointer, so '->' is not applicable ('.' is).
>
> Also, you need parentheses.


yes.. as the following code.

(obj.*pFn)();

-- Murali Krishna

 
Reply With Quote
 
Protoman
Guest
Posts: n/a
 
      07-01-2006

Murali Krishna wrote:
> > * Protoman:
> > > void (Obj::*pFn)();
> > > Obj obj;
> > > pFn=&Obj::a;
> > > obj->*pFn();

>
> Alf P. Steinbach wrote:
> > 'obj' is not a pointer, so '->' is not applicable ('.' is).
> >
> > Also, you need parentheses.

>
> yes.. as the following code.
>
> (obj.*pFn)();
>
> -- Murali Krishna


I figured that one out a couple hours ago, but my DSL and phone lines
were being upgraded, so I couldn't post right away. Anyway, can't you
use pointers to member functions to call functions based on a string
arg?

 
Reply With Quote
 
Luke Meyers
Guest
Posts: n/a
 
      07-01-2006
Protoman wrote:
> Anyway, can't you
> use pointers to member functions to call functions based on a string
> arg?


The number and type of the arguments of a member function are, of
course, irrelevant to whether you can call said member function via a
PMF. Are you asking whether you can somehow specify the name of the
function as a string, and derive a PMF therefrom? Unless you implement
it yourself, no, you cannot.

By "implement it yourself," I mean for example something like:

class Foo { void fun() {} };
typedef Foo::*() pmfFoo;
std::map<std::string, pmfFoo> lookupTable;
lookupTable["fun"] = &Foo::fun;
Foo f;
f.*lookupTable["fun"]();

I didn't compile this, so haven't found all the syntax errors, but you
get the idea.

Luke

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
static member functions vs. member function pointers paul C++ 8 04-30-2009 11:03 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
Member function pointers to member functions with default arguments Hamish C++ 3 01-25-2008 06:46 AM



Advertisments