digitalmars.D.learn - C++ cast to D cast
- BLS (24/24) Sep 22 2007 Hi, I need some advice regarding "how to translate C++ casts into D" cas...
- Bill Baxter (6/32) Sep 22 2007 I would guess
- BLS (3/41) Sep 22 2007 Thanks Bill. Yep, need more coffee
- BLS (11/11) Sep 22 2007 Hi Bill.
- Bill Baxter (7/22) Sep 22 2007 The problem I see is that cast for classes in D is like dynamic_cast<>
- BLS (7/32) Sep 22 2007 You mean
- Jarrett Billingsley (3/5) Sep 22 2007 Yessir.
- Bill Baxter (5/12) Sep 22 2007 Yes that's what I meant, but I have no idea if it works. It was just a
- Jarrett Billingsley (18/30) Sep 23 2007 class A
- BLS (8/48) Sep 23 2007 Thanks !
- Jarrett Billingsley (3/10) Sep 23 2007 Done.
- BLS (5/18) Sep 23 2007 Have to sleep, guess I'll spend myself a bottle of beer now, what
Hi, I need some advice regarding "how to translate C++ casts into D" casts. As you can see the there are a lot within the return Statement. class CGDI // C++ { public: HDC m_hDC; public: HFONT SelectFont(HFONT hFont) { return ((HFONT)::SelectObject((m_hDC), (HGDIOBJ)(HFONT)(hFont))); } HFONT SelectObject(CFont cf) { if (IsValidHandle()) return (HFONT) ::SelectObject(m_hDC,(HFONT)cf.GetFont()); return NULL; } } I guess : return (cast(HFONT)SelectObject(cast(m_hDC), cast(HGDIOBJ)cast(HFONT)cast(hFont))); But it makes no sense to me. Can you offer me some advice ? Many thanks in advance? and sorry about my ignorance ... Bjoern
Sep 22 2007
BLS wrote:Hi, I need some advice regarding "how to translate C++ casts into D" casts. As you can see the there are a lot within the return Statement. class CGDI // C++ { public: HDC m_hDC; public: HFONT SelectFont(HFONT hFont) { return ((HFONT)::SelectObject((m_hDC), (HGDIOBJ)(HFONT)(hFont))); } HFONT SelectObject(CFont cf) { if (IsValidHandle()) return (HFONT) ::SelectObject(m_hDC,(HFONT)cf.GetFont()); return NULL; } } I guess : return (cast(HFONT)SelectObject(cast(m_hDC), cast(HGDIOBJ)cast(HFONT)cast(hFont))); But it makes no sense to me. Can you offer me some advice ?I would guess return (cast(HFONT)SelectObject(m_hDC, cast(HGDIOBJ)cast(HFONT)hFont)); It's just a function call -- SelectObject(m_hDC,hFont)Many thanks in advance? and sorry about my ignorance ...Maybe you just need some sleep? :-) --bb
Sep 22 2007
Bill Baxter schrieb:BLS wrote:Thanks Bill. Yep, need more coffee BjoernHi, I need some advice regarding "how to translate C++ casts into D" casts. As you can see the there are a lot within the return Statement. class CGDI // C++ { public: HDC m_hDC; public: HFONT SelectFont(HFONT hFont) { return ((HFONT)::SelectObject((m_hDC), (HGDIOBJ)(HFONT)(hFont))); } HFONT SelectObject(CFont cf) { if (IsValidHandle()) return (HFONT) ::SelectObject(m_hDC,(HFONT)cf.GetFont()); return NULL; } } I guess : return (cast(HFONT)SelectObject(cast(m_hDC), cast(HGDIOBJ)cast(HFONT)cast(hFont))); But it makes no sense to me. Can you offer me some advice ?I would guess return (cast(HFONT)SelectObject(m_hDC, cast(HGDIOBJ)cast(HFONT)hFont)); It's just a function call -- SelectObject(m_hDC,hFont)Many thanks in advance? and sorry about my ignorance ...Maybe you just need some sleep? :-) --bb
Sep 22 2007
Hi Bill. Just something Regan and I allready have discussed ... but we are not sure about . C++ CWin* pActive= reinterpret_cast<CWin*>((HWND)::GetWindowLong(msg.hwnd,GWL_USERDATA)); D CWin pActive = cast(CWin) cast(HWND)GetWindowLong(msg.hwnd,GWL_USERDATA); where CWin is a class. The problem here is that pActive in D is a reference. Bjoern
Sep 22 2007
BLS wrote:Hi Bill. Just something Regan and I allready have discussed ... but we are not sure about . C++ CWin* pActive= reinterpret_cast<CWin*>((HWND)::GetWindowLong(msg.hwnd,GWL_USERDATA)); D CWin pActive = cast(CWin) cast(HWND)GetWindowLong(msg.hwnd,GWL_USERDATA); where CWin is a class. The problem here is that pActive in D is a reference. BjoernThe problem I see is that cast for classes in D is like dynamic_cast<> in C++. It will return null if the typeinfo system doesn't think HWND is a base class of CWin. Maybe there's some way to disable that, like by casting to void* first? Or maybe it's smarter than I'm thinking. --bb
Sep 22 2007
Bill Baxter schrieb:BLS wrote:You mean CWin pActive = cast(CWin)cast(void*) cast(HWND)GetWindowLong(msg.hwnd,GWL_USERDATA); ??? Thanks BjoernHi Bill. Just something Regan and I allready have discussed ... but we are not sure about . C++ CWin* pActive= reinterpret_cast<CWin*>((HWND)::GetWindowLong(msg.hwnd,GWL_USERDATA)); D CWin pActive = cast(CWin) cast(HWND)GetWindowLong(msg.hwnd,GWL_USERDATA); where CWin is a class. The problem here is that pActive in D is a reference. BjoernThe problem I see is that cast for classes in D is like dynamic_cast<> in C++. It will return null if the typeinfo system doesn't think HWND is a base class of CWin. Maybe there's some way to disable that, like by casting to void* first? Or maybe it's smarter than I'm thinking. --bb
Sep 22 2007
"BLS" <nanali nospam-wanadoo.fr> wrote in message news:fd2v57$1ueb$1 digitalmars.com...CWin pActive = cast(CWin)cast(void*) cast(HWND)GetWindowLong(msg.hwnd,GWL_USERDATA);Yessir.
Sep 22 2007
Jarrett Billingsley wrote:"BLS" <nanali nospam-wanadoo.fr> wrote in message news:fd2v57$1ueb$1 digitalmars.com...Yes that's what I meant, but I have no idea if it works. It was just a guess. Jarrett you can confirm that that avoids the runtime check in the cast? --bbCWin pActive = cast(CWin)cast(void*) cast(HWND)GetWindowLong(msg.hwnd,GWL_USERDATA);Yessir.
Sep 22 2007
"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message news:fd3tfr$8i6$2 digitalmars.com...Jarrett Billingsley wrote:class A { } class B { } void main() { A a = new A(); B b = cast(B)cast(void*)a; B b2 = cast(B)a; Stdout.formatln("{:X8} {:X8}", cast(void*)b, cast(void*)b2); } This prints out (something like) 0012AB60 00000000 So yes, the cast(B)cast(void*) avoids a runtime check."BLS" <nanali nospam-wanadoo.fr> wrote in message news:fd2v57$1ueb$1 digitalmars.com...Yes that's what I meant, but I have no idea if it works. It was just a guess. Jarrett you can confirm that that avoids the runtime check in the cast? --bbCWin pActive = cast(CWin)cast(void*) cast(HWND)GetWindowLong(msg.hwnd,GWL_USERDATA);Yessir.
Sep 23 2007
Jarrett Billingsley schrieb:"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message news:fd3tfr$8i6$2 digitalmars.com...Thanks ! Jarret, do you see a chance to place your solution, as well as comments at http://www.prowiki.org/wiki4d/wiki.cgi?PortingOverview Porting Code from C++ to D (Hope I am not asking for too much, but the more you are going into details the more ... You know :) BjoernJarrett Billingsley wrote:class A { } class B { } void main() { A a = new A(); B b = cast(B)cast(void*)a; B b2 = cast(B)a; Stdout.formatln("{:X8} {:X8}", cast(void*)b, cast(void*)b2); } This prints out (something like) 0012AB60 00000000 So yes, the cast(B)cast(void*) avoids a runtime check."BLS" <nanali nospam-wanadoo.fr> wrote in message news:fd2v57$1ueb$1 digitalmars.com...Yes that's what I meant, but I have no idea if it works. It was just a guess. Jarrett you can confirm that that avoids the runtime check in the cast? --bbCWin pActive = cast(CWin)cast(void*) cast(HWND)GetWindowLong(msg.hwnd,GWL_USERDATA);Yessir.
Sep 23 2007
"BLS" <nanali nospam-wanadoo.fr> wrote in message news:fd6g6h$pev$1 digitalmars.com...Thanks ! Jarret, do you see a chance to place your solution, as well as comments at http://www.prowiki.org/wiki4d/wiki.cgi?PortingOverview Porting Code from C++ to D (Hope I am not asking for too much, but the more you are going into details the more ... You know :) BjoernDone.
Sep 23 2007
Jarrett Billingsley schrieb:"BLS" <nanali nospam-wanadoo.fr> wrote in message news:fd6g6h$pev$1 digitalmars.com...Have to sleep, guess I'll spend myself a bottle of beer now, what remains : beeristasty = Muchas.Gracias.Hombre() Bjoern (I Do not speak spanish)Thanks ! Jarret, do you see a chance to place your solution, as well as comments at http://www.prowiki.org/wiki4d/wiki.cgi?PortingOverview Porting Code from C++ to D (Hope I am not asking for too much, but the more you are going into details the more ... You know :) BjoernDone.
Sep 23 2007