|
|
Scott Hill wrote:
>
> "Thorsten Froehlich" <tho### [at] trfde> wrote in message
> news:3b71858f@news.povray.org...
> >
> > I usually write operators like *, /, + outside the class.
>
> I've seen this done in many places, but have never understood why -
> these operations are properties of the class and therefore, surely, the
> correct place for them to reside is within the class ?
>
The reason for this is notation convenience. Consider class Complex with
operators *, /, +, - defined in class scope
class Complex{
public:
const Complex operator *(const Complex & c1){
//...
}
};
Now you can do this:
Complex c1, c2;
c1=c2*5;
but can't do this:
c1=5*c2;
as there is no operator *(const Complex & c) for type int. If you define
it outside of class:
const Complex operator *(const Complex &c1, const Complex &c2);
and have constructor for Complex, which allows object creation from int,
then second statement compiles OK.
Post a reply to this message
|
|