Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > VC++: Cast operator not used?

Thread Tools

VC++: Cast operator not used?

A. W. Dunstan
Posts: n/a
I'm porting some code to Visual C++ and have run into a problem - the
compiler won't use a user-written cast operator.

The code uses an envelope-letter approach to passing (potentially)
large pieces of data around, and requires that certain methods return
an Envelope with a specific kind of Letter as it's content. I have a
cast operator that converts from what I've got to what should be
returned, but it seems that the compiler only looks at constructors
for the return type.

The code works fine with the SGI MIPSPRO and Linux GCC compilers.
With MS Visual C++ .NET (v. 7.1.300 it says:

CDP3x3LinearTransform.c++(196): error C2668: 'DataPacket:ataPacket' :
ambiguous call to overloaded function
DataPacket.h(53): could be 'DataPacket:ataPacket(Letter &)'
DataPacket.h(52): or 'DataPacket:ataPacket(const DataPacket &)'
DataPacket.h(51): or 'DataPacket:ataPacket(const Envelope<T> &)'

where the offending line in CDP3x3LinearTransform.c++ looks like this:

CDP3x3LinearTransform::Output() const {
ChannelDataPacket result;
//... compute result here...
return result; // line 196

In this case I've got a ChannelDataPacket and need to return a
DataPacket. ChannelDataPacket is NOT derived from DataPacket (even
tho' you'd expect that, given their names), but they ARE related.
ChannelDataPacket DOES have a conversion operator to convert itself to
a DataPacket, however (see below). I've found two work-arounds:

#1: Call the cast operator explicitly:

return result.operator omiDataPacket();

#2: Explicitly cause the cast operator to be called by adding code:

DataPacket dp;
dp = result;
return dp;

I have two objections to both of these workarounds: a) If it won't
work as written, what's the point in having a cast operator? and b)
This is done in about 150 different places in 70 different classes.
That's just way too much typing for someone as lazy as I am.


- Is this a known problem with VC++? I've looked in the MS knowledge
base and found nothing, but I might be looking for the wrong thing.

- Is there some way of getting the VC++ compiler to notice (and use)
the cast operator without having to inflict a MS-specific kludge all
over the code?



// The Envelope to be wrapped around a Letter.
class EnvelopeBase
virtual ~EnvelopeBase();
EnvelopeBase(Letter::WriteAndCopyModeType wcm);
EnvelopeBase(const EnvelopeBase& from);
EnvelopeBase& operator=(const EnvelopeBase& rhs);

template < class T >
class Envelope : public EnvelopeBase
virtual ~Envelope();
Envelope( const Envelope& );
Envelope( Letter& letter );

Envelope& operator=( const Envelope& );
T* operator->();
const T* operator->() const;

// Letter - What gets put inside an Envelope.
class Letter
enum WriteAndCopyModeType { ValueImmediate, ValueDelayed,
Pointer };
enum ExemplarType { exemplar };
virtual ~Letter();
Letter( const String& );
Letter( const Letter& );

friend class Envelope< Letter >;

// DataPacketLetter - the data we're interested in.
class DataPacketLetter : public Letter
virtual ~DataPacketLetter();
DataPacketLetter( const DataPacketLetter& );
DataPacketLetter( ExemplarType );
DataPacketLetter( const String& );

DataPacketLetter( const String&, ExemplarType );

// DataPacket - the data we're interested in, wrapped in an Envelope.
class DataPacket : public Envelope< DataPacketLetter >
virtual ~DataPacket();
DataPacket( const Envelope< DataPacketLetter >& from );
DataPacket( const DataPacket& from );
DataPacket( Letter& letter );
DataPacket& operator=( const DataPacket& rhs );
operator DataPacketLetter&() const;

// ChannelDataPacket(Letter) - the data we're interested in, wrapped &
class ChannelDataPacketLetter : public DataPacketLetter
virtual ~ChannelDataPacketLetter();
ChannelDataPacketLetter( int theInitialSize );
ChannelDataPacketLetter( const ChannelDataPacketLetter& );
ChannelDataPacketLetter( ExemplarType );
ChannelDataPacketLetter( const String& );

class ChannelDataPacket : public Envelope< ChannelDataPacketLetter >
virtual ~ChannelDataPacket();
ChannelDataPacket( float theValue );
ChannelDataPacket( const ChannelDataPacket& theOriginal );
ChannelDataPacket( Letter& letter );
ChannelDataPacket& operator=( const ChannelDataPacket& theRhs );
ChannelDataPacket& operator=( const float& theRhs );
operator ChannelDataPacketLetter&() const;
operator DataPacket() const;
operator float() const;

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
"overloaded cast operator" and "operator const" John Goche C++ 2 09-04-2006 02:48 PM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM
malloc - to cast or not to cast, that is the question... EvilRix C Programming 8 02-14-2004 12:08 PM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM