0

I have this class:

class Object
{
    public:

    private:
        float _positionX, _positionY, _positionZ;
}

I need to do check and do some math every time a new value is assigned to them, and I also use these values in various places. I'm looking for an easier way to assign and check values than having these methods:

class Object
{
    public:
        float GetPositionX();
        void SetPositionX(float value);
        //etc
    private:
        float _positionX, _positionY, _positionZ;
}

So I'm trying to do something like this:

static class Vector3
{
    public:
        Vector3(float *valueX, float *valueY, float *valueZ)
        {
            _valueX = valueX;
            _valueY = valueY;
            _valueZ = valueZ;
        }
    private:
        float *_valueX, *_valueY, *_valueZ;
}
class Object
{
    public:
        Vector3 Position;
        //Position = Vector3(&_positionX, &_positionY, &_positionZ);
    private:
        float _positionX, _positionY, _positionZ;
}

Just so I can call it like this anywhere else in the code:

Object *myObj = new Object();
myObj->Position.x = 1; //assign, checks are done here
float myValue = myObj->Position.x; //receive, no checks needed

But the compiler complains that Vector3 doesn't have a default constructor. I also have a bunch of Object, and I'm not sure if I'm using the static Vector3 the right way (if it won't conflict with other object's values).

How can I get this working?

Danicco
  • 1,573
  • 2
  • 23
  • 49
  • 2
    Your compiler doesn't complain about missing semicolons or trying to make a static class (with a constructor yet)? – chris Apr 07 '13 at 19:50
  • According to [here](http://stackoverflow.com/questions/9321/how-do-you-create-a-static-class-in-c) there are no `static` classes in C++. – bash.d Apr 07 '13 at 19:52
  • @LuchianGrigore - According to this post (http://stackoverflow.com/questions/1635068/can-a-class-be-declared-static-in-c) `static class` is not valid – Ed Heal Apr 07 '13 at 19:53
  • I forgot to type the semicolons here, I'm sorry about that, but they're there in the code. – Danicco Apr 07 '13 at 19:53
  • @LuchianGrigore, I believe the compiler has to complain if there's no object, though. GCC certainly does. In any case, doing `class C{} static c;` might be clearer that it pertains to the object if you *really* want that all as part of that statement. – chris Apr 07 '13 at 19:54
  • VS2012 doesn't complain about the "static class", and I think if I used "static myObj" as a member, it won't work as expected, since I don't want all objects of that class to share the same static class, but instead, I want a static class so it work just as a "container/holder" of some sort. – Danicco Apr 07 '13 at 19:55
  • @bash.d A class definition can also be a declaration, and *those* can be static, e.g. `static class C{} myClass;`. – Drew Dormann Apr 07 '13 at 19:58
  • But you mean `static` as in "only visible in the translation-unit" not like in "not an instance"? – bash.d Apr 07 '13 at 19:59
  • @bash.d I think that's it, I want the class to be static so I don't have to instantiate it everytime and to be able to do something such as "obj->Position = Vector3(value1, value2, value3);" for example, with the underlying checks present in the Vector3 class, saving me a bunch of function calls, checks and typing. – Danicco Apr 07 '13 at 20:06
  • Still this does not work as in C#... You can define `static` class members, you don't Need to instantiate anything to use them. – bash.d Apr 07 '13 at 20:07

1 Answers1

1

Add a default constructor to Vector:

Vector3()
{
   _valueX = NULL;
   _valueY = NULL;
   _valueZ = NULL;
}
Miguel Prz
  • 13,718
  • 29
  • 42
  • Or call the current one from the class that has the object. – chris Apr 07 '13 at 19:56
  • Calling the non-default doesn't work because the code won't compile since I'm not calling a "Vector3 *myVector", but a "Vector3 myVector". Adding this default constructor worked perfectly (and I call the non-default in the parent class' constructor), thank you! – Danicco Apr 07 '13 at 20:14