I understand the basics of operator overloading and can make it work for the most part. However, I'm trying to do something I've only seen mentioned in passing.
I have a point defined as:
1 2 3 4 5
typedefstruct _point
{
double x, y, z;
} POINT, VERTEX;
And vector defined as a class.
I want to be able to overload the '-' operator so I can subtract two points and get a vector, but all I've seen as examples is using overloading within a class that operates on that class, even though the parameter could be a real or another vector.
I really wanted to make points and vertices a class, but was advised the such base level data is best off as a POD structure.
So, the question, how would I overload an operator to subtract two points and return a vector?
I suppose I could go back to implement points and vertices as a class, have them return a point and then use that to fill in a vector, but it seems like such a round-a-bout way to get it done.
It's read only in that I'm not doing this as a marketed application in anyway. This is solely for my personal use. As far as POD structs go, they're hardly useless and considering how hard a time I'm having trying trying to learn C++, that makes them less useless.
> As far as POD structs go, they're hardly useless and considering how hard a time
> I'm having trying trying to learn C++, that makes them less useless.
They are useful everywhere, though they are often neglected by programmers.
I suppose you know that choice of keyword struct or class does not make any difference (except in determining the default access specifier).
These three are equivalent ways to define a POD struct: typedefstruct _point { double x, y, z ; } point ;
struct point { double x, y, z ; } ;
class point { public: double x, y, z ; } ;
These three are equivalent ways to define a class that is not a POD struct: typedefstruct _book { std::string title, author ; } book ;
struct book { std::string title, author ; } ;
class book { public: std::string title, author ; } ;
JLB, I read that and keep it in mind when I decide to go struct. I generally will only use structs when the datatype has few to no member functions. I can't really think of any operation I would do on a point, except to find a vector.
The utility I'm writing takes a closed 3D geometry and does a scan of the volume. I will later use the results to calculate density and other physical properties, but there is no rotation or translation needed, so at this time, there is nothing to do with two points except find the vector defined by them.
BTW, I found the old ansi c code I had done some years back for this for this very function. I can't remember how long it took, but I suspect it took me less time writing in ansi c than it did in C++. It's no exaggeration when I say I'm having a really hard time learning C++.