C++ operator overloading to D

BLS nanali at nospam-wanadoo.fr
Sat Sep 22 14:24:56 PDT 2007


Bill Baxter schrieb:
> BLS wrote:
>> Hi,
>> I have a small problem in translating C++ operator overloading to D
>>
>> // Allready translated C++ stuff
>> class CFont
>> {
>> public:
>>   HFONT m_hFont;
>>
>>   this(HFONT hFont = NULL)
>>   {
>>     m_hFont = hFont;
>>   }
>>   ~this()
>>   {
>>     if(m_hFont !is NULL && !DeleteObject(m_hFont))
>>     m_hFont = NULL;
>>   }
>>
>> // TODO How to translate this C++ construct  into D
>>
>>     operator HFONT() {return m_hFont;}
>>
>> // That's the problem.
>>
>> Sorry have  not found anything related in the docs.
>> Some ideas;;; Many thanks in advance.
>> Bjoern
> 
> Basically you can't until D gets something like opImplicitCast.
> 
> What that does is allow the CFont class to be used in contexts where an 
> HFONT is expected.
> 
> so you need to make it into a function that explicity does the cast. 
> Such as:
> 
>      HFONT hfont() {return m_hFont;}
> 
> or name it to_HFONT() or as_hfont() or whatever tickles your fancy.

Sorry, I don't understand. But before I ask for more information, I'll 
study the C++ code again.
> 
> This is one of the bigger pains of translating C++ code to D -- the 
> repercussions of not having something like opImplicitCast.  Chances are 
> the original code will be passing CFonts around as HFONTS willy-nilly. 
> So you're going to have to add a lot of ".hfont"s  here and there to 
> make the translated code compile.
> 
> 
> --bb
Ouch!
Any plans for opImplicitCast in D 2 ?

A bit off topic
Bill, you have just translated a huge C++ project. Can you tell us a bit 
  about your experience, problems, tricks ...probabely on D WIKI.

I am going to translate about 15000 LOC (99 percent template free, 
thanks man) and I know about at least 2 guys working on bigger C++ to D 
projects. I have allready suggested to open a new forum "porting C++" 
but the feedback was not really enthusiastic, to say the least.
However, I am pretty sure that information regarding porting C++ to D is 
a matter of interest.
Bjoern




More information about the Digitalmars-d-learn mailing list