By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,420 Members | 1,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,420 IT Pros & Developers. It's quick & easy.

Pointers to member functions

P: n/a
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!!!!!

Jul 1 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* 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?
Jul 1 '06 #2

P: n/a
> * 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

Jul 1 '06 #3

P: n/a

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?

Jul 1 '06 #4

P: n/a
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

Jul 1 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.