
Is there a generic way to to initialize to zero an object representing a point in a linear space? I know the base types can be constructed like int foo(0); or double bar(0); but but if I have a three-vector class, I don't want to give it Vector::Vector(double); because Vector(2) makes no sense. (Should Vector(2) make the Vector (2, 0, 0) or the Vector (2, 2, 2)?) I also would rather have Vector() create three uninitialized doubles rather than default-constructing (0,0,0). I know SGI's extension of MonoidOperation concept has an identity_element function to go with it (see http://www.sgi.com/tech/stl/MonoidOperation.html), but that's not widely supported. So, what's the best way to initialize a generic point in a linear space to zero before, e.g., adding up a bunch of points? —Ben

Ben FrantzDale wrote:
Is there a generic way to to initialize to zero an object representing a point in a linear space? I know the base types can be constructed like int foo(0); or double bar(0); but but if I have a three-vector class, I don't want to give it Vector::Vector(double); because Vector(2) makes no sense. (Should Vector(2) make the Vector (2, 0, 0) or the Vector (2, 2, 2)?) I also would rather have Vector() create three
This is really a C++ question rather than a Boost one, if I have understood it correctly. I usually use an unparameterised constructor for this, and, indeed, there is one in the code I am working on right now: Vector::Vector(void) : x(0.0), y(0.0), z(0,0) { //any other stuff here } Does this do what you want? Paul

Paul, That isn't quite what I have in mind. (I asked on this list since it seemed most likely that Boost would have solved this problem if anyone has.) In the case of Vector, I would like to have Vector() create uninitialized memory because then Vector x[1000] takes essentially no time. But efficiency aside, the question is really how can I generically initialize a point in a linear space to the zero of that linear space. It would be nice if there were an agreed-upon PointInALinearSpace concept with a standard way of creating the zero element. One way would be to make a special zero type so then I could have these two consctructors: Vector::Vector() {} // Uninitialized members. Vector::Vector(const Zero<Vector>&) : x(0), y(0), z(0) {} —Ben On 1/22/07, Paul Giaccone <paulg@cinesite.co.uk> wrote:
Is there a generic way to to initialize to zero an object representing a point in a linear space? I know the base types can be constructed like int foo(0); or double bar(0); but but if I have a three-vector class, I don't want to give it Vector::Vector(double); because Vector(2) makes no sense. (Should Vector(2) make the Vector (2, 0, 0) or the Vector (2, 2, 2)?) I also would rather have Vector() create
Ben FrantzDale wrote: three
This is really a C++ question rather than a Boost one, if I have understood it correctly.
I usually use an unparameterised constructor for this, and, indeed, there is one in the code I am working on right now:
Vector::Vector(void) : x(0.0), y(0.0), z(0,0) { //any other stuff here }
Does this do what you want?
Paul
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

You should move your question to a newsgroup like comp.lang.c++[.moderated] where you will have more feedback and more people concerned with your question and more advices (like why you should avoid half-constructed objects and invariance maintenance). On 1/22/07, Ben FrantzDale <benfrantzdale@gmail.com> wrote:
Is there a generic way to to initialize to zero an object representing a point in a linear space? I know the base types can be constructed like
[snipped]
So, what's the best way to initialize a generic point in a linear space to zero before, e.g., adding up a bunch of points?
—Ben
best regards, -- Felipe Magno de Almeida
participants (4)
-
Ben FrantzDale
-
Felipe Magno de Almeida
-
Niitsuma Hirotaka
-
Paul Giaccone