Should be (to be 100% standard):Code:Pointer myFuncPtr = CStuff::Method;
Code:Pointer myFuncPtr = &CStuff::Method;Should be:Code:stuff.myFuncPtr(5);
(the pointer has to be dereferenced to be used)Code:stuff.*myFuncPtr(5);
Should be (to be 100% standard):Code:Pointer myFuncPtr = CStuff::Method;
Code:Pointer myFuncPtr = &CStuff::Method;Should be:Code:stuff.myFuncPtr(5);
(the pointer has to be dereferenced to be used)Code:stuff.*myFuncPtr(5);
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
I'm still getting an error:
The error is (using Dev-C++):Code:#include <iostream> using namespace std; class CStuff { public: int Method(int Arg); }; typedef int (CStuff::*Pointer)(int); int main() { Pointer myFuncPtr = &CStuff::Method; CStuff stuff; int i = stuff.*myFuncPtr(5); cout<<i<<endl; cin.get(); return 0; } int CStuff::Method(int Arg) { return Arg; }
22 C:\Documents and Settings\John Aurandt\My Documents\Source Folder\FunctPtrTest2.cpp must use .* or ->* to call pointer-to-member function in `myFuncPtr (...)'
Last edited by homeyg; 04-02-2006 at 12:13 PM.
I suggest that you go back and examine the format in my earlier post. The format Magos posted does not work for me either.
You should also take that long error message out of code tags and put it in quote tags so that that the screen width returns to a normal size.
Last edited by 7stud; 04-02-2006 at 11:55 AM.
Yeah if you put the parentheses around the dereferenced pointer in the call, it works.
Is that right? C supports all four.Should be (to be 100% standard):Code:Pointer myFuncPtr = CStuff::Method;
Code:Pointer myFuncPtr = &CStuff::Method;Should be:Code:stuff.myFuncPtr(5);Code:stuff.*myFuncPtr(5);
It seems like C++ supports all forms, too . . . .Code:user@0[~]$ cat functionpointer.c #include <stdio.h> void func(void) { puts("func()"); } int main(void) { void (*f1)(void) = func, (*f2)(void) = &func; f1(); (*f2)(); return 0; } user@0[~]$ gcc -W -Wall -ansi -pedantic -O2 -s -o functionpointer functionpointer.c user@0[~]$ cp functionpointer.c functionpointer.cpp user@0[~]$ g++ -W -Wall -ansi -pedantic -O2 -s -o functionpointer_cpp functionpointer.cpp user@0[~]$
Last edited by dwks; 04-02-2006 at 03:17 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
We're talking about member functions for classes, so C is irrelevant.Originally Posted by dwks
have a read of the function pointer tutorials.
you might also want to have a look at boost.function.
it's much more flexible and has a nicer syntax.
which would you rather use?
This does come at a small performance cost but the flexibility and readability far outweight this IMNSHO.Code:class MyClass { public: void foo(float x, char y, char z) { } }; int (MyClass::*pt2Member)(float, char, char) = &MyClass::foo; MyClass x; (x.*pt2Member)(1.0, 'a', 'b'); // or MyClass x; boost::function<void> f = boost::bind(&MyClass::foo, &x); f(1.0, 'a', 'b');
Last edited by ChaosEngine; 04-02-2006 at 08:16 PM. Reason: forgot the class definition
"I saw a sign that said 'Drink Canada Dry', so I started"
-- Brendan Behan
Free Compiler: Visual C++ 2005 Express
If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?
You said:We're talking about member functions for classes, so C is irrelevant.
Both work. And both are "100% standard".Should be (to be 100% standard):Code:Pointer myFuncPtr = CStuff::Method;
Code:Pointer myFuncPtr = &CStuff::Method;
Besides, C has everything to do with it. The reason C++ has two different syntaxes for the same thing is that it was inherited from C. The reason C has both methods is a long story.
In C, and C++, given func is declared as, &func is the same as func, and func() is the same as (*func)(). It's part of the language.Code:void (*func)(void)
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I read somewhere (when searching for info on member-pointers) that leaving the & out is non-standfard, and that GCC doesn't even allow it to be missing. I don't have GCC though so I cannot confirm it.Both work. And both are "100% standard".
Plain function pointers, sure. But method pointers need the (C.*M)() rather than C.M(), or the compiler (in my case MSVC2003) thinks I'm trying to call a non-existing method rather than use the pointer.and func() is the same as (*func)()
And yes the parantehses needs to be there, forgot it in my earlier post.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
true, but as has already been mentioned, we're not talking about free function pointers here, we're talking about member function pointers, which have a different syntax.Originally Posted by dwks
"I saw a sign that said 'Drink Canada Dry', so I started"
-- Brendan Behan
Free Compiler: Visual C++ 2005 Express
If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?
Dev-C++ won't compile when you useOriginally Posted by dwks
orCode:funcPtr = Blah::func;
Code:myClass.*funcPtr(blah);