www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Translating parts of WTL to D

reply Lionello Lunesu <lio lunesu.remove.com> writes:
How would you translate this class to D:

#class _U_MENUorID
#{
#public:
#  _U_MENUorID(HMENU hMenu) : m_hMenu(hMenu)
#  { }
#  _U_MENUorID(UINT nID) : m_hMenu((HMENU)(UINT_PTR)nID)
#  { }	
#  HMENU m_hMenu;
#};
#void testfunc( _U_MENUorID x = 0U ){}

The straightforward translation would be:

#class _U_MENUorID
#{
#public:
#  this(HMENU hMenu) { m_hMenu=(hMenu);
#  }
#  this(UINT nID) { m_hMenu=(cast(HMENU)cast(UINT_PTR)nID);
#  }	
#  HMENU m_hMenu;
#}
#void testfunc( _U_MENUorID x = null ){}

but it's not quite as usable as the C++ version.

Any ideas?

L.
May 18 2006
next sibling parent Lionello Lunesu <lio lunesu.remove.com> writes:
Try 2:

#struct _U_MENUorID {
#  static _U_MENUorID opCall(HMENU hMenu) {
#    _U_MENUorID u;
#    u.m_hMenu = hMenu;
#    return u;
#  }
#  static _U_MENUorID opCall(UINT nID) {
#    _U_MENUorID u;
#    u.m_hMenu = cast(HMENU)nID;
#    return u;
#  }
#  HMENU m_hMenu;
#}
#void testfunc( _U_MENUorID x = _U_MENUorID(null) ){}

It'll be efficient (no new's, pass by val) but needs an explicit cast 
(for both the default value and every passed parameter).

L.
May 18 2006
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Lionello Lunesu" <lio lunesu.remove.com> wrote in message 
news:e4hiuv$1af1$1 digitaldaemon.com...

 but it's not quite as usable as the C++ version.

Huh? How so?
May 18 2006
parent Lionello Lunesu <lio lunesu.remove.com> writes:
Jarrett Billingsley wrote:
 "Lionello Lunesu" <lio lunesu.remove.com> wrote in message 
 news:e4hiuv$1af1$1 digitaldaemon.com...
 
 but it's not quite as usable as the C++ version.

Huh? How so?

Take somefunc for example. C++: #void testfunc( _U_MENUorID x = 0U ){ # CreateWindow( ..., x.m_hMenu ); #} #void usage(){ # testfunc(); # testfunc(IDM_MAINMENU); # testfunc(hMenu); #} with the 'straightforward' D conversion I need an extra check in the function. Also, I must create instances of the class first :( #void testfunc( _U_MENUorID x = null ){ # CreateWindow( ..., x?x.m_hMenu:null ); #} #void usage(){ # testfunc(); # testfunc( new _U_MENUorID(IDM_MAINMENU) ); # testfunc( new _U_MENUorID(hMenu) ); #} L.
May 18 2006