CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2010
    Posts
    115

    Question Pointer to member function

    Is there an actual advantage of using a pointer to member function ?

    I think the syntax looks no good at all ? I can't comprehend much.

    Code:
    struct A
    {
        void somefunc(){}
    };
    
    void A::somefunc(int i)
    {
        int (*uglyPointI)(void*);
        uglyPointI=i;
        //do something
        (*uglyPointI)((void*)&i);
    }
    Please, don't ask me what I am doing because I actually have no idea what I am doing either. I know I am making a bad question and I hope you would correct it and help me make a better question in the future...Thank you..

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: Pointer to member function

    Is there an actual advantage of using a pointer to member function ?
    If you need one, it would be advantageous to have them.

    I think the syntax looks no good at all ?
    Admittedly, it seemed confusing to me the first time I glanced them, although I don't see any pointer to member functions in your example.

    Code:
    class TestClass
    {
    public:
        int someMemberFunction(int n){ return n; }
    };
    
    int (TestClass::*memberFunctionPointer)(int) = &TestClass::someMemberFunction;
    
    TestClass t;
    
    int result = (t.*memberFunctionPointer)(42)
    There is also std::function, which can be used to "point" to both functions and member functions.

    Please, don't ask me what I am doing because I actually have no idea what I am doing either.
    Perhaps you should wait until you do know what you are doing?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured