Thread: function pointers in c++ classes

  1. #1
    optimism_
    Guest

    Question function pointers in c++ classes

    Hiya.

    Im making my first game in c++ with DirectX, but at the moment i'm working on the control engine. I wanted to use function pointers to call a function specific to an object, an instance of my objects class. I have made a message stack, but want to have each message handled differently for each object i instaciate. Follow?

    Any way, i'm a little stumpes on how to call this function, or even how to set it up. Currently, the prototype inside the class is

    int (DefProcedure*)(UINT Msg, WPARAM wParam, LPARAM lParam);

    But how would i then call this? So far the compiler has thrown back an error for every time i've tried to set the pointer to a function. Methods i've tried:

    player.DefProcedure = proc();
    CPlayer:efProcedure = proc();
    DefProcedure = proc();

    even
    player.DefProcedure() = proc()

    yet everyone has been rejected. Please can someone correct this sow i can continue with the engine. Thx

    optimism_

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Calling a function pointer in C++ is ugly. So the best way to do it is to encapsulate it in a friendlier package.

    Code:
    class Player { public:
    
    int (Player:: * DefProcedure)(UINT Msg, WPARAM wParam, LPARAM lParam);
    
    int Handle(UINT Msg, WPARAM wParam, LPARAM lParam){
    (*this.*DefProcedure)(Msg, wParam, lParam);
    }
    int Kill(UINT Msg, WPARAM wParam, LPARAM lParam){
     //...
    }
    int ShowMercy(UINT Msg, WPARAM wParam, LPARAM lParam){
     //...
    }
    Player(){
    DefProcedure = &Player::ShowMercy;
    }
    };
    
    // syntax:
    
    player.Handle(msg, wParam, lParam);
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    optimism_
    Guest
    Thx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM