Re: function pointer class member ?
On Sep 16, 7:58 am, sledge <sunilk...@gmail.com> wrote:
Hi,
I am porting some legacy C code to C++.
If that is so, you should know first rule of porting, and that is:
"Don't". Porting is risky, and C++ is awesome enough that you can wrap
existing code (put a C++ fa=E7ade on it) virtually without impacting
performance. Did you try that first?
The code has a lot of member
function pointers like
class X {
int (*a)(int,int);
void (*b)(char t);
int x;
int f(int,int);
};
main()
{
// declare a pointer to member function
void (X::*a) (int) = &X::f;
This does not seem to work. What is the right syntax to handle the
function pointer?
Obviously, this should be void (X::*a) (int, int) = &X::f; (note the
second "int").
On a more general note...
Your C function pointers do not seem to belong to X (logically, not
formally). If they "belonged", they would typically look like so:
int (*a)(X* this, int param1, int param2); (they would have "explicit"
this pointer.
That said, function pointers like these are normally a sign of an
attempt to get polymorphism in C (and if so, the above is not the best
version). If that is the purpose of the code, then you should just
switch to virtual functions.
Goran.