how to use a member function inside a function pointer?

This is a discussion on how to use a member function inside a function pointer? within the C++ Programming forums, part of the General Programming Boards category; hiya, this function poitner is located on another class class b { void Push(void (*State)()); }; so, class a { ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    142

    how to use a member function inside a function pointer?

    hiya,

    this function poitner is located on another class

    class b {
    void Push(void (*State)());
    };

    so,

    class a {
    b;
    void functiontopush();
    void somefunction();
    }

    void a::somefunction()
    {
    b.push(functiontopush) // error,
    }

    cannot convert parameter 1 from 'void (void)' to 'void (__cdecl *)(void)'

    thanks!

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    First of all your code doesnt compile at all, and it has nothing to do with the fact that you are trying to make a function pointer of a class member function. Secondly, I would guess that there is some easier and more appropriate way to do what you are trying to do, especially given your level of coding. Thirdly, the main problem with passing a member function pointer is that you need an instance of the object to call the function, which you are not providing. The compiler is complaining because (if you are using microsoft at least) the calling convention is __thiscall for classes, and the argument to the function is a __cdecl calling convention prototype. If you really want to pass a class member function, there is some thread a few days or a week back that I answered about how to do it, but it involves a lot of templates, function objects, and generally very ugly code. I think it had something about string::find_last_of or string::find_first_of in the title.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    c++ provides pointer to member operators just for this sort of thing.... they are...
    ::*
    ->*
    .*

    Most text books at least tell you about these. Look them up. They have uses for pointers to member funcs.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    ehm, i have a stack class that stores functions. This stack class i shall place on my main class which where my program runs.

    ie,
    class CStack
    {
    ...
    }

    Class MainProgram
    {
    CStack m_stackState;
    ProgramState1();
    }

    I'm trying to follow how people progarm in java, they have everything inside a main class etc., now i just want to push the ProgramState1() inside my stack class, plz help, you said something different ways? could you advice me more?

    many thanks, searching for your thread btw..,

    StonedCoder: hiya, i tried all those it won't compile,

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    hiya, oke i made my function a static now it works without casting or additional changing on the code but static?? i don't want it static or else i have to make my member variables static as well right? help? again, ifyousayso, plz advice me on a better alternative, thanks!

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    hiya, anyone? i can't find the solution or is it even possible? someone says that it's only the compiler's extension and other stuffs like address won't be known etc.,

    so am i stuck with just making it static? thanks!

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Stoned_Coder
    c++ provides pointer to member operators just for this sort of thing.... they are...
    ::*
    ->*
    .*
    ::* is not an operator.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing pointer inside function?
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2008, 06:10 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21