how call a method through an function pointer?

Discussion in 'C++' started by dennis luehring, Apr 8, 2005.

  1. i try to "publish" some of my very stupid methods (i need this for an interpreter)

    function_one
    procedure_one

    here is the code

    ---------
    class core
    {
    public:
    int result;
    int parameter[2];

    typedef void(core::*call)();
    typedef struct method_t
    {
    bool function; // additional information
    call method; // the call
    };

    method_t methods[2];

    core()
    {
    methods[0].function = true;
    methods[0].method = function_one;

    methods[1].function = false;
    methods[1].method = procedure_one;
    }

    // callable methods
    void function_one(){ result = parameter[0] + parameter[1]; }
    void procedure_one(){ parameter[0] = 10; }
    };

    void main()
    {
    core c;

    // why can't i call the method...
    //c.methods[0].method();
    // can't compile this - error
    // error C2064: term does not evaluate to a function taking 0 arguments

    // ...should be the same as
    c.function_one();
    }
     
    dennis luehring, Apr 8, 2005
    #1
    1. Advertising

  2. dennis luehring

    Jason Guest

    dennis luehring wrote:
    > ---------
    > class core
    > {
    > public:
    > int result;
    > int parameter[2];
    >
    > typedef void(core::*call)();
    > typedef struct method_t
    > {
    > bool function; // additional information
    > call method; // the call
    > };
    >
    > method_t methods[2];
    >
    > core()
    > {
    > methods[0].function = true;
    > methods[0].method = function_one;
    >
    > methods[1].function = false;
    > methods[1].method = procedure_one;
    > }
    >
    > // callable methods
    > void function_one(){ result = parameter[0] + parameter[1]; }
    > void procedure_one(){ parameter[0] = 10; }
    > };
    >
    > void main()
    > {
    > core c;
    >
    > // why can't i call the method...
    > //c.methods[0].method();
    > // can't compile this - error
    > // error C2064: term does not evaluate to a function taking 0

    arguments
    >
    > // ...should be the same as
    > c.function_one();
    > }


    Pointers to member functions are in the FAQ, but I can't help but think
    that there must be a better way... Maybe polymorphism? What are you
    trying to achieve?

    http://www.parashift.com/c -faq-lite/pointers-to-members.html

    -Jason
     
    Jason, Apr 8, 2005
    #2
    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. glen stark
    Replies:
    2
    Views:
    727
    Ron Natalie
    Oct 10, 2003
  2. Replies:
    2
    Views:
    435
    Larry Brasfield
    Feb 17, 2005
  3. Larry Brasfield
    Replies:
    0
    Views:
    383
    Larry Brasfield
    Feb 17, 2005
  4. Bruce.
    Replies:
    20
    Views:
    731
    Asm23
    Jun 25, 2007
  5. Pawel_Iks
    Replies:
    3
    Views:
    553
    terminator
    Jul 31, 2007
Loading...

Share This Page