-
May 15th, 2013, 11:13 AM
#1
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;
?
Thanks in advance!
-
May 15th, 2013, 11:21 AM
#2
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
-
May 15th, 2013, 04:36 PM
#3
Re: Member function pointer assignment
Ah yes. Now I remember another thing the compiler wants me to do is
, not just .
Thank you!
-
May 16th, 2013, 06:22 AM
#4
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. 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.?
-
May 16th, 2013, 08:35 AM
#5
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").
-
May 16th, 2013, 12:18 PM
#6
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?
-
May 17th, 2013, 08:42 PM
#7
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.
-
May 18th, 2013, 12:06 AM
#8
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.
-
May 20th, 2013, 09:33 AM
#9
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?
-
May 20th, 2013, 10:34 AM
#10
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;
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 20th, 2013, 01:42 PM
#11
Re: Member function pointer assignment
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
-
May 20th, 2013, 03:06 PM
#12
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.
Last edited by acppdummy; May 20th, 2013 at 03:08 PM.
-
May 20th, 2013, 03:17 PM
#13
Re: Member function pointer assignment
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 20th, 2013, 03:40 PM
#14
Re: Member function pointer assignment
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.
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
-
May 20th, 2013, 07:05 PM
#15
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|