Re: New type apear as a TColor ?


2007-12-11 09:43:30 PM
cppbuilder92
Matthias Pätzold < XXXX@XXXXX.COM >writes:
Quote
>>TColor TAjColor::operator TColor()
>>{
>>long int c;
>>
>>c = Alpha;
>>c << 8;
>>c += Blue;
>>c << 8;
>>c += Green;
>>c << 8;
>>c += Red;
>>return (TColor)c;
>>};
>This gives the error:
>E2036 Conversion operator cannot have a return type specification
What if you use reinterpret_cast?
That's not the problem. It's the function signature:
TColor TAjColor::operator TColor()
^^^^^^
|
|
remove
It's called "operator TColor()" which already says what type it
returns, so you're not supposed to specify a return type in addition.
Imagine the confusion if the following was allowed?
class x
{
public:
long operator int() const; // invalid
};
What does operator int() return? A long??? No, operator int() must
return an int, and does not require (or allow) an explicit return
type:
class x
{
public:
operator int() const; // correct
};
It would be redundant to be forced to say:
class x
{
public:
int operator int(); // invalid, redundant
};
So they just drop the return type for conversion operators.
I do strongly caution AGAINST using conversion operators except in
very, very specific cases where it can be justified. These do tend to
open the door to surprises and incorrect usages that silently compile
and are a monster to debug.
--
Chris (TeamB);