Don't return a reference to myclass, just return a myclass. You
usually only return references when you are returning one of the
original arguments to the operator, such as when implementing a stream
operator for a iostream and a custom class.
myclass myclass::operator+(const myclass& x)
{
myclass temp;
temp= ...something...
return(temp);
}
On 4/19/06, Michal Seliga <[EMAIL PROTECTED]> wrote:
> did anyone succeeded with this?
>
> i have following problem:
> when i override for example operator + i must return result.
>
> in all c++ tutorial i found its easy
>
> myclass& myclass::operator+(const myclass& x)
> {
> myclass temp;
>
> temp= ...something...
> return(temp);
> }
>
> this doesn'twork in metrowerks (actually i will also get warning during
> compilation)
>
> so another approach, i make temp=new myclass and return *temp
>
> this also isn't good idea because allocation will leak and it will never be
> deleted
>
> i tried some more things but still nothing usable... only thing which i think
> is
> usable needs a lot of coding and it is called garbage collector, and it is
> definitely not what i would like to write... will i have to?
>
> --
>
> what i am writing is actually class for fixed point math. in database we work
> with quantities and prices. so we need to handle millions, possibly billions
> and
> also to have valid 4 decimal places. so far we use double type, but results
> are
> very bad ( something like 5-4=0.999999999 and so on).
>
> idea was that i will create my own type and change all 'double' to this.
> problem
> is that i don't like to rewrite all calculations to something like
> temp=qty;temp*=price;temp*=discount;temp/=100;
>
> when original was temp=qty*price*discount/100, i think everyone understand
> why ;-)
>
> is there any good and working approach how to makeit possible?
>
>
>
> another possibility is to use long long type and have all numbers multiplied
> by
> 10000 and handle this correctly when making multiplications and divisions. but
> problem is that this code should be portable and long long is not supported
> properly everywhere (at the moment i am not sure if it was pocket pc team or
> c++
> builder which told me this). so i would use it only if everything else will
> fail
>
> any other idea?
>
> --
> For information on using the PalmSource Developer Forums, or to unsubscribe,
> please see http://www.palmos.com/dev/support/forums/
>
--
For information on using the PalmSource Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/support/forums/