views:

89

answers:

2

Hi, I'm creating a minimal circle-circle physics engine in a static library, and I've came across a problem. I have a header file for object manipulation, one of the variables is the objects position. The position variable is declared in bpObject.h, and I have a void function, SetPosition(), that accesses the current position and sets its to the parameters specified (also declared in bpObject.h). My problem is that in the source file, I need to access the position variable (private). I can't access it through the bpObject class, because, being a class, it won't have the correct value when it is used as a type. So, how would I access the position variable within the class?

Thanks in advanced,

Greg Treleaven

EDIT

Code for bpObject.h

#include "bpMath.h"

namespace bp
{
    class Object
    {
    private:
    static bp::Vector position;
    static bp::Vector velocity;
    static bp::Vector acceleration;
    public:
    static single restitution;
    static single radius;
    static void setPosition(single X, single Y);
    static bp::Vector getPosition();
    static void applyPosition(single X, single Y);
    static void setVelocity(single X, single Y);
    static bp::Vector getVelocity();
    static void applyVelocity(single X, single Y);
    static void setAcceleration(single X, single Y);
    static bp::Vector getAcceleration();
    static void applyAcceleration(single X, single Y);
     }
}
+3  A: 

I'm guessing you don't actually want all those 'static's in there, is your first problem (as it stands, you pretty much can only access a single object)

Once you get rid of those, you can implement SetPosition in your source file by:

namespace bp {
    void Object::SetPosition(single X, single Y) {
        position[0] = X; //or however your bp::Vector is implemented
        position[1] = Y;
    }
}

Yes, position is private, but when you actually define the method, you get to access the members. Is this at all what you are asking?

Matthew Hall
Thanks, that was what I was asking. 1-up, and tick!
Greg Treleaven
A: 

You're asking the wrong question. If, as you say, the variable doesn't have the correct value when it's needed, then the problem has nothing to do with access methods or public/private.

How is the variable supposed to get its proper value? Something must call setPosition, so you have to arrange things so that this happens before anything else needs that value.

Once you have that, your accessors (setPosition and getPosition) should work just fine (after you get rid of the static, which makes no sense).

Beta