Member function pointer assignment
I have a class with member functions and a pointer like this:
Code:
class MyClass{
private:
typedef void(MyClass::*memFnPtr_t) ();
public:
memFnPtr_t fnptr;
void fn1();
void fn2();
};
Now in a regular function I create an instance of the class and try to assign the pointer:
Code:
MyClass mc;
mc.fnptr = &mc.fn1;
The compiler does not like it and suggests this instead:
Code:
mc.fnptr = &MyClass::fn1;
This seems to work but what if I have two instances of the class:
How does the compiler know to distinguish between
and
when the assignment now looks identical:
Code:
mc1.fnptr = &MyClass::fn1;
mc2.fnptr = &MyClass::fn1;
?
:confused:
Thanks in advance!
Re: Member function pointer assignment
You supply to instance to use when calling the function pointer.
http://www.newty.de/fpt/fpt.html#call
gg
Re: Member function pointer assignment
Ah yes. Now I remember another thing the compiler wants me to do is
, not just .
Thank you! :)
Re: Member function pointer assignment
Wait! When calling the function pointer it is just calling the function pointer, not the actual function. So it's still not clear to me how and are distinguished. :confused: Does
Code:
mc1.fnptr = &MyClass::fn1;
imply and similarly
Code:
mc2.fnptr = &MyClass::fn1;
imply , such that the same R.H.S. means different things depending on the different L.H.S.?
Re: Member function pointer assignment
the function pointer is "without instance" (it is static). it's only when calling the function that you need to supply the instance.
so mc1.fn1 == mc2.fn1 == MyClass::fn1. All 3 approaches return the same value ( a pointer to a class memberfunction that requires a class instance as it's "hidden first parameter").
Re: Member function pointer assignment
Thanks! So if I understand it correctly, when calling from mc1,
(this->*fnptr)()
calls
mc1.fn1
and
(mc2.*fnptr)()
calls
mc2.fn1
right?
Re: Member function pointer assignment
Another question: can the assignment be done in the constructor? (to initialize the pointer)
I tried it but kept getting core dump. :confused:
Re: Member function pointer assignment
Yes, the function can be parsed into the constructor.
Code:
class MyClass
{
typedef void(MyClass::*memFnPtr) ();
public:
MyClass(memFnPtr funcptr)
: fnptr(funcptr)
{
// fnptr = funcptr; // Also can go here instead of initialiser list
// Constructor
};
memFnPtr fnptr;
void fn1();
void fn2();
};
void MyClass::fn1()
{
// Function 1 called!
};
void MyClass::fn2()
{
// Function 2 called!
};
Then your declaration would be like
Code:
MyClass mc1(&MyClass::fn1), mc2(&MyClass::fn2);
Some examples of calling it
Code:
(mc1.*(mc1.fnptr))(); // Call function 1 from class mc1
(mc1.*(mc2.fnptr))(); // Call function 2 from class mc1
(mc2.*(mc1.fnptr))(); // Call function 1 from class mc2
(mc2.*(mc2.fnptr))(); // Call function 2 from class mc2
There are 2 parts you're looking at here:
Part 1: (mc1.*(mc2.fnptr))();
Part 2: (mc1.*(mc2.fnptr))();
Part 1 is the object calling the function, so either mc1 or mc2.
Part 2 is the function address type. mc1.fnptr just returns &MyClass::fn1 which we assigned in the constructor. Likewise, mc2.fnptr is just returning &MyClass::fn2. You could replace the following to achieve the same result:
Code:
(mc1.*(&MyClass::fn1))(); // Call function 1 from class mc1
(mc1.*(&MyClass::fn2))(); // Call function 2 from class mc1
Hope that helps.
Re: Member function pointer assignment
Thank you! But I tried both
Code:
public:
MyClass(): fnptr(&MyClass::fn1){
...
};
and
Code:
public:
MyClass(): {
fnptr=&MyClass::fn1;
};
. In both cases I got core dumps at run time (compiler was happy). :( What did I do wrong?
Re: Member function pointer assignment
How have you used them? because it works OK for me. The following code compiles and runs with MSVS.
Code:
#include <iostream>
using namespace std;
class MyClass
{
typedef void(MyClass::*memFnPtr) ();
public:
MyClass(memFnPtr funcptr)
: fnptr(funcptr) {}
MyClass()
: fnptr(&MyClass::fn1) {}
memFnPtr fnptr;
void fn1();
void fn2();
};
void MyClass::fn1()
{
// Function 1 called!
cout << "func1" << endl;
}
void MyClass::fn2()
{
// Function 2 called!
cout << "func2" << endl;
}
int main() {
MyClass mc1(&MyClass::fn1),
mc2(&MyClass::fn2),
mc3;
(mc1.*(mc1.fnptr))(); // Call function 1 from class mc1
(mc1.*(mc2.fnptr))(); // Call function 2 from class mc1
(mc2.*(mc1.fnptr))(); // Call function 1 from class mc2
(mc2.*(mc2.fnptr))(); // Call function 2 from class mc2
(mc1.*(&MyClass::fn1))(); // Call function 1 from class mc1
(mc1.*(&MyClass::fn2))(); // Call function 2 from class mc1
(mc1.*(mc3.fnptr))(); // Call function 1 from class mc1
(mc2.*(mc3.fnptr))(); // Call function 1 from class mc2
(mc3.*(mc3.fnptr))(); // Call function 1 from class mc3
(mc3.*(&MyClass::fn2))(); // Call function 2 from class mc3
return 0;
}
Re: Member function pointer assignment
Quote:
Originally Posted by
acppdummy
In both cases I got core dumps at run time (compiler was happy). :( What did I do wrong?
If you posted a full program, then we can tell you what you did wrong, if the compiler is buggy, etc.
Regards,
Paul McKenzie
Re: Member function pointer assignment
Well it's a bit complicated: the function pointer that points to either fn1 or fn2 is used inside another member function of the same class like this:
(this->*fnptr)()
and fn1 and fn2 actually has an argument that's a pointer to a privately declared/defined type within the same class. So more like this:
(this->*fnptr)(&var);
I ended up doing the assignment to the function pointer outside of the class and that seems to work. It's just when I tried to initialize it in the constructor that I got core dumps.
Re: Member function pointer assignment
Quote:
Originally Posted by
acppdummy
Well it's a bit complicated: I ended up doing the assignment to the function pointer outside of the class and that seems to work. It's just when I tried to initialize it in the constructor that I got core dumps.
So do what Paul requested in post #11 and post a complete program that demonstrates the issue.
Re: Member function pointer assignment
Quote:
Originally Posted by
acppdummy
Well it's a bit complicated: the function pointer that points to either fn1 or fn2 is used inside another member function of the same class like this:
(this->*fnptr)()
and fn1 and fn2 actually has an argument that's a pointer to a privately declared/defined type within the same class. So more like this:
(this->*fnptr)(&var);
So what is stopping you from posting this program? It doesn't sound complicated to me, or at the very least, the problem could be easily duplicated with a small example.
Quote:
It's just when I tried to initialize it in the constructor that I got core dumps.
If you don't show the code that fails, you'll never get the reason why it fails.
Regards,
Paul McKenzie
Re: Member function pointer assignment
Thank you everyone for offering to help! I will first try to duplicate the problem with a small example. If that doesn't work then I will zip up the full program and upload. Thanks again!