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);
Printable View
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);
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;
}
Quote:
22 C:\Documents and Settings\John Aurandt\My Documents\Source Folder\FunctPtrTest2.cpp must use .* or ->* to call pointer-to-member function in `myFuncPtr (...)'
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.
Yeah if you put the parentheses around the dereferenced pointer in the call, it works.
Is that right? C supports all four.Quote:
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[~]$
We're talking about member functions for classes, so C is irrelevant.Quote:
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');
You said:Quote:
We're talking about member functions for classes, so C is irrelevant.
Both work. And both are "100% standard".Quote:
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)
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.Quote:
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.Quote:
and func() is the same as (*func)()
And yes the parantehses needs to be there, forgot it in my earlier post.
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.Quote:
Originally Posted by dwks
Dev-C++ won't compile when you useQuote:
Originally Posted by dwks
orCode:funcPtr = Blah::func;
Code:myClass.*funcPtr(blah);