Topic: Help needed: Conversion operator


Author: jcoffin@taeus.com (Jerry Coffin)
Date: 1998/12/02
Raw View
In article <36643BAF.6C873983@club-internet.fr>, lehong@club-
internet.fr says...

[ ... ]

> - Can someone compile it using their favorite compiler to see what
> happens. I'm particularly interested in the results obtained using MSVC6
> and EGCS.

VC++ 6.0 SP1 compiles the code with no errors or warnings.

egcs 1.1 gives the following error:
conversion.cpp: In function `void TryConvert()':
conversion.cpp:62: call of overloaded `DateTime(U &)' is ambiguous
conversion.cpp:8: candidates are: DateTime::DateTime(const DateTime &)
conversion.cpp:7:                 DateTime::DateTime(long int)

where line 62 is the second to last line and line 8 is the closing
brace of the DateTime class in your snippet.

If you eliminate the cast, and simply use:

     DateTime lDT2 = /*(DateTime)*/ lUDT; // <== Error here

either compiler will accept the code without errors and only giving
warnings about unused variables/functions, which are typical under the
circumstances.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Phuc LE HONG <lehong@club-internet.fr>
Date: 1998/12/01
Raw View
I have some C++ sources that has been compiled using Borland C++ 3.5 and
MSVC 4.2. It also compiles on Solaris with CC 4.1. Now, I'm trying to
compile them using MSVC 5 + SP3, without success. It seems that the
problem lie in an ambiguous use of conversion operator.

I've written a small excerpt that shows the problem. So:

- Is there any language lawyer that can tell me whether the code is
wrong or if its a compiler bug ?

- Can someone compile it using their favorite compiler to see what
happens. I'm particularly interested in the results obtained using MSVC6
and EGCS.

Here is the source code. Thanks.

==================================================================================
// Conversion Problem

class DateTime
{
     long Time;
   public:
     DateTime(long aTime) : Time(aTime) {}
};

enum UValType { UVT_LONG, UVT_DATETIME };

class UValBase
{
     UValType Type;
   public:
     UValBase(UValType aType) : Type(aType) {}
     UValType GetType() const { return Type; };
};

class UValLong : public UValBase
{
     long ValLong;
   public:
     UValLong(long aLong) : UValBase(UVT_LONG), ValLong(aLong) {}
     long GetLong() const { return ValLong; }
};

class UValDateTime : public UValBase
{
     DateTime ValDateTime;
   public:
     UValDateTime(const DateTime &aDT)
          : UValBase(UVT_DATETIME), ValDateTime(aDT)
     {}
     const DateTime & GetDateTime() const { return ValDateTime; }
};

class U
{
     union
     {
          UValBase *pBase;
          UValLong *pLong;
          UValDateTime *pDateTime;
     };

   public:
     U(long aLong) { pLong = new UValLong(aLong); }
     U(const DateTime &aDT) { pDateTime = new UValDateTime(aDT); }

     operator long() { return pLong->GetLong(); }
     operator DateTime() { return pDateTime->GetDateTime(); }
};

void TryConvert()
{
     U lULong(3L);
     DateTime lDT(1000000L);
     U lUDT(lDT);

     long lVal = lULong;
     DateTime lDT2 = (DateTime) lUDT; // <== Error here
     DateTime lDT3 = lUDT;      // <== Another error
}
==================================================================================



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]