function pointer

This is a discussion on function pointer within the C++ Programming forums, part of the General Programming Boards category; I am a little confused on function pointers. I am trying to pass a function to handle the movement of ...

  1. #1
    NotSoAvgProgrammer
    Join Date
    Jul 2007
    Location
    Virginia, U.S.
    Posts
    57

    function pointer

    I am a little confused on function pointers. I am trying to pass a function to handle the movement of a paddle, the function either returning an array of floats for a user (key presses) or for ai. I'm sure the class is still pretty bad, but I'm just trying to get it running right now. I have gotten it all down to one error, and that's foo is not defined.

    Any help, bee it with foo, or anything else that comes to your attention would be greatly appreciated.

    Code:
    class Paddle4f
    {
    public:
    	Paddle4f(float length, float width, float height);
    	void move();
    	void setMoveFunc(float* MoveFunc(void));
    	void reset(float z);  // how far on z, differ from user and ai
    				// again, public so collision can access it
    	struct info{
    		float x, y, z;
    		float w, l, h;
    		float* velocity;
    		float speed[3];
    		int score;    //keeps track of each paddles score
    	}stats;
    private:
    	float* (*foo)(void);
    };
    
    Paddle4f::Paddle4f(float length, float width, float height)
    {
    	stats.l = length;
    	stats.w = width;
    	stats.h = height;
    }
    void Paddle4f::reset(float z)
    {
    	stats.x = 0.0f;
    	stats.y = 0.0f;
    	stats.z = z;
    }
    
    void setMoveFunc(float* (*moveFunc)(void))
    {
    	*foo = &moveFunc;
    }
    
    void Paddle4f::move()
    {
    	stats.velocity = foo();
    }

    Thank you for your time and effort,

    Joe
    A quality job is not necessarily a *good* job. A quality job is, by definition, conformance to requirements. Therefore a *good* job, may not be a quality job.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Just
    foo = moveFunc;
    should suffice. But your prototype and implementation head do not match.

    But you do realize that the move function can do exactly nothing? It doesn't have any access to the paddle. It doesn't have any reference to the real world. It's a function callback without any context data. That's pretty much the worst thing you can do.

    I also just don't see the point of the thing.

    On another note, the data design of the class is weird.
    First, it's not at all encapsulated - all data is right there in the open.
    Second, it's inconsistent. On one hand, you have the two individually named triples for position and size, on the other you have the array triple for speed. All three should probably be some sort of Vector3f object.
    Also, it is entirely unclear what the difference between velocity and speed is.
    Then there's the issue of how velocity is to be interpreted. That's completely unclear, too. Who is responsible for the pointer? How many floats does it point to?

    And why the name Paddle4f? I don't see where it takes 4 floats as arguments.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 08:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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