 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Radek Guest
|
Posted: Sat Dec 17, 2005 7:39 pm Post subject: How to overload multiplication operator for a structure? |
|
|
Hi all,
I am currently trying to boost performance of my code which uses complex
template. I want to compare performance of STL complex and my defined
coomplex using structure like this:
struct my_complex { double re; double im };
I want to overload multiplication operator for such a struct. How to do it?
my_complex operator* (my_complex x , my_complexy) { return (x.re * y.re +
x.re * y.im + x.im * y.re - x.im * y.im); }
Is it ok or I should change sth in the code?
Br,
Radek
|
|
| Back to top |
|
 |
Ed Mulroy Guest
|
Posted: Sat Dec 17, 2005 9:03 pm Post subject: Re: How to overload multiplication operator for a structure? |
|
|
| Quote: | my_complex operator* (my_complex x , my_complex y)
{
return (x.re * y.re + x.re * y.im + x.im * y.re - x.im * y.im);
}
|
That computes a double and returns it. That double is not the product of
the two complex numbers.
The operator should return a my_complex structure which is the product.
It accepts the calling argument by value, forcing copies to be created and
placed onto the stack. Passing by const reference is more efficient.
Something like this might be closer to what you want:
my_complex operator * (const my_complex & mc1, const my_complex & mc2)
{
my_complex result;
result.re = mc1.re * mc2.re - mc1.im * mc2.im;
result.im = mc1.re * mc2.im + mc1.im * mc2.re;
return result;
}
.. Ed
| Quote: | Radek wrote in message
news:43a4693e$1 (AT) newsgroups (DOT) borland.com...
Hi all,
I am currently trying to boost performance of my code which uses
complex template. I want to compare performance of STL complex
and my defined coomplex using structure like this:
struct my_complex { double re; double im };
I want to overload multiplication operator for such a struct. How to do
it?
my_complex operator* (my_complex x , my_complexy) { return (x.re * y.re +
x.re * y.im + x.im * y.re - x.im * y.im); }
Is it ok or I should change sth in the code?
|
|
|
| Back to top |
|
 |
Radek Guest
|
Posted: Sat Dec 17, 2005 9:18 pm Post subject: Re: How to overload multiplication operator for a structure? |
|
|
| Quote: | my_complex operator* (my_complex x , my_complex y)
{
return (x.re * y.re + x.re * y.im + x.im * y.re - x.im * y.im);
}
That computes a double and returns it. That double is not the product of
the two complex numbers.
The operator should return a my_complex structure which is the product.
|
Yeah. I have already noticed my mistake :-)
| Quote: | It accepts the calling argument by value, forcing copies to be created and
placed onto the stack. Passing by const reference is more efficient.
Something like this might be closer to what you want:
my_complex operator * (const my_complex & mc1, const my_complex & mc2)
{
my_complex result;
result.re = mc1.re * mc2.re - mc1.im * mc2.im;
result.im = mc1.re * mc2.im + mc1.im * mc2.re;
return result;
}
|
That's what I wanted. Thanks very much for your help. I will try that out.
Thanks again,
Radek
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|