|
-
May 20th, 2013, 10:34 AM
#11
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)
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
|