0

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 advance,

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);
     }
}
Juergen
  • 12,378
  • 7
  • 39
  • 55
Greg Treleaven
  • 8,124
  • 7
  • 30
  • 30
  • 1
    You need to add actual (minimal) code that represents your problem. – dash-tom-bang Sep 03 '10 at 18:45
  • Why not add a member to `bpObject` class to get back the current position? – dirkgently Sep 03 '10 at 18:45
  • If the Object class has the position as a member variable and the SetPosition function is also a member of the Object class, then you should have no problem manipulating the position. Show some code so that we can see what you are actually having problems with. – TheUndeadFish Sep 03 '10 at 18:48
  • "...being a class, it won't have the position variable contained when it is used as a type." If you mean that the variable won't have the correct *value* at the time that it's needed, then you have a more serious design problem than you realize. If you mean it literally, then I suspect you just don't know how classes work. – Beta Sep 03 '10 at 18:53
  • @Beta: I did mean the former, yes. – Greg Treleaven Sep 03 '10 at 18:55
  • 2
    is it on purpose that all methods and members of `Object` are static ? This resembles more the functionality of a namespace rather than a class (e.g. you can only have one `position` in your entire program etc.) – Andre Holzner Sep 03 '10 at 19:02
  • 2
    Why do you have so many things declared `static`? Why even bother to have a class if everything is going to be a `static`? Why not just have a namespace? I too suspect you misunderstand how classes work and what they're for. – Omnifarious Sep 03 '10 at 19:07
  • @Omnifarious: You'll give me a lecture about how classes work and what they are for? The tutorial I followed to get me started had static for everything, i've taken them out now as i've seen they cause problems. – Greg Treleaven Sep 03 '10 at 19:25
  • 2
    @Greg: My suggestion about the tutorial you used: find something else, and post where you got the tutorial so we can warn other people against it. There's an excellent book list at http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list, and free books, some of them for C++, at http://stackoverflow.com/questions/194812/list-of-freely-available-programming-books. Anything on there is likely to be better than what you're using now. – David Thornley Sep 03 '10 at 19:51

2 Answers2

3

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?

Gretchen
  • 2,274
  • 17
  • 16
0

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
  • 96,650
  • 16
  • 149
  • 150