From: David Wilkinson on 1 Feb 2010 09:59 Igor Tandetnik wrote: >> UINT_PTR n = 42; >> LPVOID p = reinterpret_cast<LPVOID>(n); >> >> Many thanks. This has fixed the problem though I'm not sure why I can't just >> do LPVOID(n)? > > You can. Personally, I prefer the notation using type conversion operator, but a functional cast ( LPVOID(n) ) or a C-style cast ( (LPVOID)n ) should both work, too. Igor: Ah, yes. I was thinking of LPVOID p(n); which does not work. But I guess I am confused. I have always thought of what you call "functional cast" as invoking the constructor. So why exactly do LPVOID p = LPVOID(n); LPVOID p(LPVOID(n)); both work, while LPVOID p = n; LPVOID p(n); do not? -- David Wilkinson Visual C++ MVP
From: Bo Persson on 1 Feb 2010 11:44 David Wilkinson wrote: > Igor Tandetnik wrote: >>> UINT_PTR n = 42; >>> LPVOID p = reinterpret_cast<LPVOID>(n); >>> >>> Many thanks. This has fixed the problem though I'm not sure why I >>> can't just do LPVOID(n)? >> >> You can. Personally, I prefer the notation using type conversion >> operator, but a functional cast ( LPVOID(n) ) or a C-style cast >> ( (LPVOID)n ) should both work, too. > > Igor: > > Ah, yes. I was thinking of > > LPVOID p(n); > > which does not work. > > But I guess I am confused. I have always thought of what you call > "functional cast" as invoking the constructor. It can invoke a constructor, it the type has one. For built-in types it is the same as a C-style cast. > So why exactly do > > LPVOID p = LPVOID(n); > LPVOID p(LPVOID(n)); Because these are casts. > > both work, while > > LPVOID p = n; > LPVOID p(n); > > do not? And these are not. LPVOID doesn't have a converting constructor. Bo Persson
From: Igor Tandetnik on 1 Feb 2010 11:41 David Wilkinson <no-reply(a)effisols.com> wrote: > But I guess I am confused. I have always thought of what you call > "functional cast" as invoking the constructor. When a functional cast expression has one parameter, it is exactly equivalent to a corresponding C-style cast. Yes, you can invoke a constructor using C-style cast: class C { public: C(int); }; C v = (C)42; > So why exactly do > > LPVOID p = LPVOID(n); > LPVOID p(LPVOID(n)); > > both work, while > > LPVOID p = n; > LPVOID p(n); > > do not? Well, because the standard says so. I don't know the rationale behind this, but if I had to guess, I'd say the committee made functional cast and C-style cast behave the same in order to avoid the proliferation of similar but subtly different cast mechanisms. -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
First
|
Prev
|
Pages: 1 2 Prev: enum operator overloading VC9 compiler bug? Next: Rectangular objects from client area |