BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to overload multiplication operator for a structure?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Radek
Guest





PostPosted: Sat Dec 17, 2005 7:39 pm    Post subject: How to overload multiplication operator for a structure? Reply with quote



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





PostPosted: Sat Dec 17, 2005 9:03 pm    Post subject: Re: How to overload multiplication operator for a structure? Reply with quote



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





PostPosted: Sat Dec 17, 2005 9:18 pm    Post subject: Re: How to overload multiplication operator for a structure? Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.