problem using function pointers

Discussion in 'C++' started by Irtech, Oct 30, 2012.

  1. Irtech

    Irtech Guest

    Hello,

    I have a class that has a function pointer member. for assigning functions to function pointer I use a public function "SetCallback".
    at the construction time I want to assign a default function to my function pointer called "DefaultCallBack".
    Unfortunately I couldn't do that till now because I get compiler errors.
    I use VC++ in visual studio 2012 which supports c++11.

    here is my code:
    http://pastebin.com/CzxnnGPQ
    Can you please tell me why this code does not compile ?
    thanks.
     
    Irtech, Oct 30, 2012
    #1
    1. Advertising

  2. Irtech

    red floyd Guest

    On 10/30/2012 12:40 PM, Irtech wrote:
    > Hello,
    >
    > I have a class that has a function pointer member. for assigning functions to function pointer I use a public function "SetCallback".
    > at the construction time I want to assign a default function to my function pointer called "DefaultCallBack".
    > Unfortunately I couldn't do that till now because I get compiler errors.
    > I use VC++ in visual studio 2012 which supports c++11.
    >
    > here is my code:
    > http://pastebin.com/CzxnnGPQ


    You have an error on line 42 of your code.
    Seriously, though. Post the smallest *COMPILABLE* example here,
    along with the specific error message you are getting.

    We're not going to click on some random link.

    See FAQ 5.8
     
    red floyd, Oct 30, 2012
    #2
    1. Advertising

  3. Irtech

    Werner Guest

    On Tuesday, October 30, 2012 9:40:28 PM UTC+2, Irtech wrote:
    > Hello,
    >
    >
    >
    > I have a class that has a function pointer member. for assigning functions to function pointer I use a public function "SetCallback".
    > I use VC++ in visual studio 2012 which supports c++11.
    >
    >
    >
    > here is my code:


    > using std::function;
    > class command
    > {
    > function<void(enum_command_status)> m_callbackStatusChange;
    > void SetCallback(function<void (enum_command_status)> callbackNew
    > {m_callbackStatusChange = callbackNew;}
    > void DefaultCallBack(enum_command_status){delete this;}
    > }


    Missing a ";" here above...

    > command::command()
    > {
    > SetCallback(DefaultCallBack);
    > }


    What a surprise the user of this class will get...

    int main()
    {
    command c;
    c.SetCallback( xxx ); //Crash boom bang!
    }
     
    Werner, Oct 31, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil
    Replies:
    1
    Views:
    661
    llewelly
    Sep 16, 2003
  2. muser
    Replies:
    3
    Views:
    776
    Ron Natalie
    Sep 18, 2003
  3. Peter Goddard

    void pointers & void function pointers

    Peter Goddard, May 16, 2005, in forum: C Programming
    Replies:
    3
    Views:
    527
    Peter Goddard
    May 16, 2005
  4. n2xssvv g02gfr12930

    Smart pointers and member function pointers

    n2xssvv g02gfr12930, Nov 26, 2005, in forum: C++
    Replies:
    3
    Views:
    480
    n2xssvv g02gfr12930
    Nov 27, 2005
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    696
Loading...

Share This Page