From: Pat on
Is it possible to define overloaded operators for structures?

What I want to do is define a structure to represent cartesian points.

ie

struct Point
{
double x;
double y;
};

and then be able to add, substract, and multiply points like they were
scalar variables.

ie.

Point p1 = {1,2);
Point p2 = {4,6};

Point p3 = p1+p2; <-- this is what I would like to be able to do
Point p4 = 5*p1; <--

Is this possible? This is mainly for convenience, but would really
simplify some of the code I need to write.

Thanks,

Pat
From: Kaz Kylheku on
On 2009-12-01, Pat <pkelecy@_REMOVETHIS_gmail.com> wrote:
> Is it possible to define overloaded operators for structures?

Yes, in C++, structs are classes.

They can have constructors, destructors, virtual functions,
inheritance, etc.

> Point p1 = {1,2);
> Point p2 = {4,6};
>
> Point p3 = p1+p2; <-- this is what I would like to be able to do

This looks exactly like the addition of two complex
numbers. C++ has complex numbers!

Complex numbers are ideal for representing points on a two-dimensional plane,
and doing mapping transformations which have a geometric interpretation.

For instance, you can rotate a complex number by multiplying it
by another complex number.

> Point p4 = 5*p1; <--

Complex numbers do that: multiplication by a real/scalar to scale the vector.
From: Keith Thompson on
Pat <pkelecy@_REMOVETHIS_gmail.com> writes:
> Is it possible to define overloaded operators for structures?

In which language?

In standard C, no. In C++, yes; consult any C++ textbook.

--
Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
From: Pat on
Kaz Kylheku wrote:
> On 2009-12-01, Pat <pkelecy@_REMOVETHIS_gmail.com> wrote:
>> Is it possible to define overloaded operators for structures?
>
> Yes, in C++, structs are classes.
>
> They can have constructors, destructors, virtual functions,
> inheritance, etc.

That's good to know! So are they defined the same way as with classes?
I have three books on C++ that I'm using for reference, and the
examples they give on operating overloading all involve class
definitions. I haven't seen any that specifically apply it to a
structure.

>
>> Point p1 = {1,2);
>> Point p2 = {4,6};
>>
>> Point p3 = p1+p2; <-- this is what I would like to be able to do
>
> This looks exactly like the addition of two complex
> numbers. C++ has complex numbers!
>
> Complex numbers are ideal for representing points on a two-dimensional plane,
> and doing mapping transformations which have a geometric interpretation.
>
> For instance, you can rotate a complex number by multiplying it
> by another complex number.
>
>> Point p4 = 5*p1; <--
>
> Complex numbers do that: multiplication by a real/scalar to scale the vector.

Yes, you're right. The vector calculations I need to perform could be
done in terms of complex numbers. I didn't realize C++ could support
that. Again, that's good to know, and definitely an option. It all
comes down to which would be easier to implement. I currently have
everything worked out in terms of pure vector relations (additions,
subtractions, dot products, multiplication/division by a scalar), but to
rewrite those in terms of complex variables should be pretty trivial.
I'll probably try both ways and see which one I like better.

Thanks for the help!

Pat
From: Pat on
Keith Thompson wrote:
> Pat <pkelecy@_REMOVETHIS_gmail.com> writes:
>> Is it possible to define overloaded operators for structures?
>
> In which language?
>
> In standard C, no. In C++, yes; consult any C++ textbook.
>

Yes, I'm working in C++.

I have several C++ books, and the overloading examples they show all
involve class definitions. Do you know of any texts you could recommend
that have good examples of this being applied to structures?