|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Dec 6, 8:00 pm, dolphin <jdxyw2...@gmail.com> wrote:
> 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....f7add269?tvc=2 Short answer: It can not be a member because the member would never get invoked! |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On 12ÔÂ6ÈÕ, ÏÂÎç11ʱ32·Ö, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote: > On Dec 6, 8:00 pm, dolphin <jdxyw2...@gmail.com> wrote: > > > > > > > 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/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? |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Thu, 6 Dec 2007 07:00:20 -0800 (PST) in comp.lang.c++, dolphin
<jdxyw2004@gmail.com> wrote, >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. } |
|
![]() |
| Outils de la discussion | |
|
|