none
Save function pointer in class member function RRS feed

  • Question

  • Hi,

    Please look at the below class:

    //test.h

    class test{

    public:

    static void Init(void(*cbvalueChanged)(void));

    ...

    };

    // test.cpp

     

    void test::Init(void(*cbvalueChanged)(void))
    {
    // here I am asked to just save the callback funtion pointer, please guide me how to achieve this...??

    // this function pointer is defined in some other class, that I am not aware.

    }

    I do not have any other inputs apart from this.

    I am new to this function pointer concept.

    Looking for the help to save the funptr.

    Thanks in advance,

    Regards,

    Mayank


    Thanks & Regards, Mayank Agarwal

    Thursday, June 23, 2016 9:07 AM

Answers

  • Hi,

    Please look at the below class:

    //test.h

    class test{

    public:

    static void Init(void(*cbvalueChanged)(void));

    ...

    };

    // test.cpp

     

    void test::Init(void(*cbvalueChanged)(void))
    {
    // here I am asked to just save the callback funtion pointer, please guide me how to achieve this...??

    // this function pointer is defined in some other class, that I am not aware.

    }

    I do not have any other inputs apart from this.

    I am new to this function pointer concept.

    Looking for the help to save the funptr.

    Thanks in advance,

    Regards,

    Mayank


    Thanks & Regards, Mayank Agarwal

    It's handy to define a type for the pointer:

    typedef void (*cbvalueChanged_ptr_type)(void);

    you can declare a variable to store the pointer:

    cbvalueChanged_ptr_type m_function_pointer;

    you can store the pointer:

    void Init(cbvalueChanged_ptr_type ptr)
    {
        m_function_pointer = ptr;
    }
    

    • Marked as answer by MayankAg Thursday, June 23, 2016 10:18 AM
    Thursday, June 23, 2016 9:39 AM

All replies

  • Hi,

    Please look at the below class:

    //test.h

    class test{

    public:

    static void Init(void(*cbvalueChanged)(void));

    ...

    };

    // test.cpp

     

    void test::Init(void(*cbvalueChanged)(void))
    {
    // here I am asked to just save the callback funtion pointer, please guide me how to achieve this...??

    // this function pointer is defined in some other class, that I am not aware.

    }

    I do not have any other inputs apart from this.

    I am new to this function pointer concept.

    Looking for the help to save the funptr.

    Thanks in advance,

    Regards,

    Mayank


    Thanks & Regards, Mayank Agarwal

    It's handy to define a type for the pointer:

    typedef void (*cbvalueChanged_ptr_type)(void);

    you can declare a variable to store the pointer:

    cbvalueChanged_ptr_type m_function_pointer;

    you can store the pointer:

    void Init(cbvalueChanged_ptr_type ptr)
    {
        m_function_pointer = ptr;
    }
    

    • Marked as answer by MayankAg Thursday, June 23, 2016 10:18 AM
    Thursday, June 23, 2016 9:39 AM
  • Thanks a ton BlueLed,

    But do i need to declare this "cbvalueChanged_ptr_type" also somewhere?
    Please guide.

    Thanks & Regards, Mayank Agarwal

    Thursday, June 23, 2016 9:53 AM
  • Thanks a ton BlueLed,

    But do i need to declare this "cbvalueChanged_ptr_type" also somewhere?
    Please guide.

    Thanks & Regards, Mayank Agarwal


    It's up to you to find where to put the typedef, based on the structure of your project. At least, make sure it doesn't get included more than once in the same compilation unit.
    Thursday, June 23, 2016 10:03 AM
  • This worked, Thank you so much.. :)

    Thanks & Regards, Mayank Agarwal

    Thursday, June 23, 2016 10:18 AM