A question about operator overloading?

D

dolphin

Hi All
I just read the C++FAQ . I have something confused. The question
is that :

If you provide constructive operators, they should allow promotion of
the left-hand operand (at least in the case where the class has a
single-parameter ctor that is not marked with the explicit keyword).
For example, if your class Fraction supports promotion from int to
Fraction (via the non-explicit ctor Fraction::Fraction(int)), and if
you allow x - y for two Fraction objects, you should also allow 42 -
y. In practice that simply means that your operator-() should not be a
member function of Fraction. Typically you will make it a friend, if
for no other reason than to force it into the public: part of the
class, but even if it is not a friend, it should not be a member.


Why the operator-() should not be a member function? Can someone give
a example?
 
A

Abhishek Padmanabh

Hi All
I just read the C++FAQ . I have something confused. The question
is that :

If you provide constructive operators, they should allow promotion of
the left-hand operand (at least in the case where the class has a
single-parameter ctor that is not marked with the explicit keyword).
For example, if your class Fraction supports promotion from int to
Fraction (via the non-explicit ctor Fraction::Fraction(int)), and if
you allow x - y for two Fraction objects, you should also allow 42 -
y. In practice that simply means that your operator-() should not be a
member function of Fraction. Typically you will make it a friend, if
for no other reason than to force it into the public: part of the
class, but even if it is not a friend, it should not be a member.

Why the operator-() should not be a member function? Can someone give
a example?

Aah, another thread with the same issue. See this one -
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/13801063f7add269?tvc=2

Short answer: It can not be a member because the member would never
get invoked!
 
D

dolphin

Aah, another thread with the same issue. See this one -http://groups.google.com/group/comp.lang.c++/browse_thread/thread/138...

Short answer: It can not be a member because the member would never
get invoked!- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -

Do you mean that operator+(T , T) and operator*(T,T) also should not
be member function?
 
D

David Harmon

On Thu, 6 Dec 2007 07:00:20 -0800 (PST) in comp.lang.c++, dolphin
Why the operator-() should not be a member function? Can someone give
a example?

#include <complex>
int main()
{
std::complex<double> v(0.5);
v - 1.0; // works whether or not - is a member
1.0 - v; // Works only if - is a non-member.
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,200
Latest member
LaraHunley

Latest Threads

Top