C++ operator overloading to D

Bill Baxter dnewsgroup at billbaxter.com
Sat Sep 22 13:26:39 PDT 2007


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.

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


More information about the Digitalmars-d-learn mailing list