Member function pointer assignment
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: Member function pointer assignment

  1. #1
    Join Date
    Nov 2010
    Posts
    105

    Question 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:
    Code:
    MyClass mc1, mc2;
    How does the compiler know to distinguish between
    Code:
    mc1.fn1
    and
    Code:
    mc2.fn1
    when the assignment now looks identical:
    Code:
    mc1.fnptr = &MyClass::fn1;
    mc2.fnptr = &MyClass::fn1;
    ?



    Thanks in advance!

  2. #2
    Join Date
    Nov 2003
    Posts
    1,795

    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

  3. #3
    Join Date
    Nov 2010
    Posts
    105

    Re: Member function pointer assignment

    Ah yes. Now I remember another thing the compiler wants me to do is
    Code:
    (this->*fnptr)()
    , not just
    Code:
    fnptr()
    .

    Thank you!

  4. #4
    Join Date
    Nov 2010
    Posts
    105

    Question 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
    Code:
    mc1.fn1
    and
    Code:
    mc2.fn1
    are distinguished. Does
    Code:
    mc1.fnptr = &MyClass::fn1;
    imply
    Code:
    &mc1.fn1
    and similarly
    Code:
    mc2.fnptr = &MyClass::fn1;
    imply
    Code:
    &mc2.fn1
    , such that the same R.H.S. means different things depending on the different L.H.S.?

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,827

    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").

  6. #6
    Join Date
    Nov 2010
    Posts
    105

    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?

  7. #7
    Join Date
    Nov 2010
    Posts
    105

    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.

  8. #8
    Join Date
    May 2013
    Posts
    18

    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.

  9. #9
    Join Date
    Nov 2010
    Posts
    105

    Question 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?

  10. #10
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,316

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Member function pointer assignment

    Quote Originally Posted by acppdummy View Post
    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

  12. #12
    Join Date
    Nov 2010
    Posts
    105

    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.

  13. #13
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,316

    Re: Member function pointer assignment

    Quote Originally Posted by acppdummy View Post
    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  14. #14
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Member function pointer assignment

    Quote Originally Posted by acppdummy View Post
    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

  15. #15
    Join Date
    Nov 2010
    Posts
    105

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center