lazy/quick point class question

i recently started with vector graphics. i've never made use of OO functions in audio programming, and thought i'd better try it the OO way instead of a C/array approach.

the STL was indicated to me a few days ago, i guess containers are the way to go for dynamic object/entity implementation. ...i think i'm gonna stick with a simple homemade object array for my first project. reading around, it seems like many game programmers write their own classes. the STL is also a load to absorb just to switch to a class implementation of points.. (my first project is 2d.. tho i've done 3d the C/array way).

i've been reading all these nice tutorials about vector operations, dot products and so forth, and how tidy and clean the code is because you don't have to write three commands to add two 3d vectors, you just write a += b;

great! then, i find out that actually writing a point class isn't trivial. a 1D point would be simple for the first venture into using classes. of course, it would also be totally besides the point.

it's my bedtime after a while looking, and it seems that no one on the internet is handing out a free point class i can "just drop into my code without learning about first.." ;) (i'm old and allowed to be facetious)

so i'm wondering, i guess for my program to recognise vector addition like a += b; i need to know about... overloading??? or do i need to go as far as templates and inheritance before i can implement a 2d point and say a += b???

every time i've read the section over the past years i haven't been able to see a single reason why anyone would ever want to overload anything..




(nb. there are plenty of published point classes.. here's one..

class vector3 {
public:
float x, y, z;
inline vector3 operator + (const vector3 &param) const {
return vector3(x + param.x, y + param.y, z + param.z);
}
inline vector3& operator += (const vector3 &param) {
x += param.x; y += param.y; z += param.z;
return *this;
}
inline vector3 operator * (float param) const {
return vector3(x * param, y * param, z * param);
}
// Lots of other methods here
};

but for instance, i have no reference on the & keyword and so forth... all it really tells me is that it's way over my head.



edit: and adding to my basic question ("is this overloading? do i need to reread templates before i get there?") i wonder - given the above example where the + and += operators are defined, is it then necessary to also define the - and -= operators, or does c++ see these as + and += when doing this?
Last edited on
so i'm wondering, i guess for my program to recognise vector addition like a += b; i need to know about... overloading??? or do i need to go as far as templates and inheritance before i can implement a 2d point and say a += b???
Yes, you need operator overloading. Needing is kind of inappropriate as you could simply do vector.add(vector) or vector.multiplyByScalar(number) but operator overloading provide a nicer way to write that.
Templates for a point class are used to automatically generate the versions with int, float and double coordinates (the most used) but it's not strictly necessary. Considering that you will most likely only use two of the versions (an integer one an a decimal one) you could write them by hand and provide a way to cast them.

but for instance, i have no reference on the & keyword and so forth
I believe that is only the HTML way to write &.

is it then necessary to also define the - and -= operators
Yes it is. You'll just have to copy the addition operators and change the plus to minus.
Last edited on
tyvm, it was a lazy question but your objectivity allowed me to focus on what i needed without being worn down for the day by review.

(schildt's beginners, indeed object overloading uses a point as an example, everything makes much more sense now that i have an application..)

i appreciate the confirmation and wish there were some way to thank for answers without bumping threads!
Topic archived. No new replies allowed.