Operator overloading.

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
typedef struct _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.
1
2
3
4
typedef struct _point
{
	double x, y, z;
} POINT, VERTEX;
That's a C thing to do. In C++ it is completely useless.

Anyway, you can write global operators:
1
2
3
4
return_type operator some_op(const typeA &left, const typeB &right)
{
    //
}


See http://www.cplusplus.com/doc/tutorial/classes2/ , there is a table halfway down with the list of operators that can be globally overloaded.
Last edited on
closed account (D80DSL3A)
You should be able to do it.
1
2
3
4
5
vector point::operator-(const point& b) const
{
    // construct a_vector from the 2 points
    return a_vector;
}
Last edited on
fun2code, that requires that there be a prototype for it in the struct, which he may not be capable of doing if it is, for instance, read only code.
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:
typedef struct _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:
typedef struct _book { std::string title, author ; } book ;

struct book { std::string title, author ; } ;

class book { public: std::string title, author ; } ;

closed account (D80DSL3A)
Please disregard this irrelevant post. It was senseless.
Last edited on
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++.
Last edited on
roberts I mean that using typdef for structs was useless.
Instead of:
1
2
3
4
 typedef struct tagMyStruct
{
    //
} MYSTRUCT, *PMYSTRUCT;
It is better to do:
1
2
3
4
struct MyStruct
{
    //
};
Topic archived. No new replies allowed.