PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > A question about operator overloading?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
A question about operator overloading?

Réponse
 
LinkBack Outils de la discussion
Vieux 06/12/2007, 15h00   #1
dolphin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut A question about operator overloading?

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?
  Réponse avec citation
Vieux 06/12/2007, 15h32   #2
Abhishek Padmanabh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: A question about operator overloading?

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!
  Réponse avec citation
Vieux 07/12/2007, 01h56   #3
dolphin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: A question about operator overloading?

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?
  Réponse avec citation
Vieux 07/12/2007, 06h08   #4
David Harmon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: A question about operator overloading?

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.
}
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h48.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12925 seconds with 12 queries