VC++ 5.0 ambiguity: conversion operator vs overloaded operator? 
Author Message
 VC++ 5.0 ambiguity: conversion operator vs overloaded operator?

Hi,

ARM says "User defined conversions are used implicitly only if they
are unambiguous" (12.3.2).

In the following code, if I define a operator+(int), VC used that in
preference to operator int().  If I take operator+(int) out, compiler
happily uses operator int().  

VC5 clearly has a precedence algorithm of some kind in situations
where either an overloaded operator or a conversion operator could be
used.  But that sems to contradict ARM a bit. Is that precedence rule
part of C++?  Do you get same result on other compilers?

I'd appreciate a reply via e-mail as well as this group, as my
new server is a bit flaky.

Remember to remove the antispam alias in my mail address.

Thanks

Alan

//----------------------------------------------------------------------

#include <iostream>

using std::cout;

class   MyClass
{
 public:

   MyClass() : m_myMember(0)
   {cout << "in MyClass()\n";}  

   MyClass(int i) : m_myMember(i)
   {cout << "in MyClass(int)\n";}  

   MyClass(MyClass& aMyClass)
   {m_myMember = aMyClass.m_myMember;
    cout << "in MyClass(MyClass&)\n";}  

// apparently takes precedence over operator int() in situation
// MyClass dMyClass = aMyClass + 22; // used operator+(int anInt) if
defined

   MyClass operator+(int anInt)
   {cout << "in operator+(int)\n";
    return MyClass(anInt + m_myMember);}

   operator int()
   {cout << "in operator int()\n";
    return m_myMember;}

//virtual ~MyClass();  // accept default destructor

 protected:

 private:
  int m_myMember;

Quote:
};

//////////////////////////////////////////////////////////////////

int main()
{
 MyClass aMyClass = MyClass(23);

/*

if operator+(int) AND operator int() defined, uses
operator+(int): output is:

  in MyClass(int)
  in operator+(int)
  in MyClass(int)

if ONLY operator int() defined, output is:

  in MyClass(int)
  in operator int()
  in MyClass(int)

*/

 MyClass dMyClass = aMyClass + 22; // used operator+(int anInt) if
defined

 return 0;

Quote:
}

I'd appreciate replies via e-mail as well as this group, as
my newsserver is a bit flaky...but don't forget to remove
"antispam." from my address...

Yours,

Alan Campbell
Brighton, UK

<<<<<<<<<<<<<<<<<<<<<<     >>>>>>>>>>>>>>>>>>>



Fri, 13 Apr 2001 03:00:00 GMT  
 VC++ 5.0 ambiguity: conversion operator vs overloaded operator?

Quote:

>VC5 clearly has a precedence algorithm of some kind in situations
>where either an overloaded operator or a conversion operator could be
>used.  But that sems to contradict ARM a bit. Is that precedence rule
>part of C++?  Do you get same result on other compilers?

    There is no conflict or ambiguity.

    When operator+(int)  is defined, this is no implicit conversion taking
place.  You are very explicitly saying to use MyClass::operator+(int).  That
is always the first choice of the compiler.

    When operator+ is not defined,  then MyClass + int has no meaning, and
the compiler must find a way to make the statement work.  It discovers that
it can (implicitly) convert a MyClass into an int,  and then add that int to
another int.

    The rule you cite would only come into play if you replaced
MyClass::operator+(int) with a global ::operator+(MyClass, MyClass).  In
that case, the compiler would have the choice of converting the right-hand
side to a MyClass, and calling that operator, or converting the left-hand
side into an int, and called the native operation.  In that case, the line
would be a compile-time error.  (I believe, but am not certain, that the
compiler would accept the line if given MyClass::operator+(MyClass) as a
member function, implicitly converting the int to a MyClass)

--
Truth,
   James [MVP]
http://www.NJTheater.Com       -and-
http://www.NJTheater.Com/JamesCurran

begin 666 James M. Curran.vcf


M4DLZ.SLQ."!*;VAN(%-T+"!3=6ET92 R0CM";&]O;69I96QD.TY*.S W,# S
M+34Q-#D-"DQ!0D5,.U=/4DL[14Y#3T1)3D<]455/5$5$+5!224Y404),13HQ
M."!*;VAN(%-T+"!3=6ET92 R0CTP1#TP04)L;V]M9FEE;&0L($Y*(# W,# S
M+34Q-#D-"E523#IH='1P.B\O=W=W+DY*5&AE871E<BYC;VTO2F%M97-#=7)R
M86X-"E523#IH='1P.B\O=W=W+DY*5&AE871E<BYC;VT-"D5-04E,.U!2148[
M24Y415).150Z2F%M97-#=7)R86Y 0V]M<'5397)V92YC;VT-"E)%5CHQ.3DX

`
end



Fri, 13 Apr 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. == and != operator (operator overloading)

2. Help:overloading delete operator in vc++ 4.0

3. Help:overloading delete operator in vc++ 4.0

4. Resolving overload Template operator<<() with VS 2003

5. ++ operator: 4.2 vs 5.0

6. A problem compiling DLLs with protected operator= in VC++ 5.0 (C2248)

7. problem with arithmetic if operator in VC++ 5.0

8. VC++ 5.0, optimizations, and ::operator new

9. Using C# operator overloading in VB.NET

10. Can't overload operator new in VC7.

11. HELP: Overloading Assignment Operators in MC++

12. Overloading Assignment Operators in MC++

 

 
Powered by phpBB® Forum Software