www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C++ operator overloading to D

reply BLS <nanali nospam-wanadoo.fr> writes:
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
Sep 22 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
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
Sep 22 2007
parent reply BLS <nanali nospam-wanadoo.fr> writes:
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
Sep 22 2007
parent Bill Baxter <dnewsgroup billbaxter.com> writes:
BLS wrote:
 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.
operator SOMETYPE() in C++ is a casting/conversion operator. http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=24&rl=1 So with your example, given some API like SetFont(HFONT font); That operator HFONT makes it so you can pass a CFont to that function as if it were an HFONT. CFont mycfont; ... SetFont(mycfont); // first calls operator HFONT() and passes result to SetFont
 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 ?
Apparently so according to WalterAndrei.pdf.
 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 was thinking about it, but I wasn't sure if anyone would be interested.
 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.
To me the D newsgroup is a fine place to discuss porting issues. And a wiki with advice would be nice -- especially for people who want to port something but don't really know C++ that well to begin with.
 However, I am pretty sure that information regarding porting C++ to D is 
 a matter of interest.
Well, if you think so I can jot down some of my observations about porting from C++ to D on wiki4d. --bb
Sep 22 2007