Discussion:
Convert from UINT_PTR to LPVOID
(too old to reply)
Nic
2010-02-01 05:02:24 UTC
Permalink
Hello
how can I convert from UINT_PTR to LPVOID?
Thanks.
Nic
Igor Tandetnik
2010-02-01 05:29:15 UTC
Permalink
Post by Nic
how can I convert from UINT_PTR to LPVOID?
UINT_PTR n = 42;
LPVOID p = reinterpret_cast<LPVOID>(n);
--
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
Nic
2010-02-01 05:59:37 UTC
Permalink
Post by Nic
how can I convert from UINT_PTR to LPVOID?
UINT_PTR n = 42;
LPVOID p = reinterpret_cast<LPVOID>(n);
--
With best wishes,
Igor Tandetnik

Many thanks. This has fixed the problem though I'm not sure why I can't just
do LPVOID(n)?
Nic.
David Wilkinson
2010-02-01 10:25:55 UTC
Permalink
Post by Igor Tandetnik
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)?
Because UINT_PTR is not a pointer. It is an unsigned integer type with the same
size as a pointer. So you have to do an explicit cast.
--
David Wilkinson
Visual C++ MVP
Igor Tandetnik
2010-02-01 12:37:10 UTC
Permalink
Post by Igor Tandetnik
Post by Nic
how can I convert from UINT_PTR to LPVOID?
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.
--
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
David Wilkinson
2010-02-01 14:59:30 UTC
Permalink
Post by Igor Tandetnik
Post by Igor Tandetnik
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
Bo Persson
2010-02-01 16:44:22 UTC
Permalink
Post by David Wilkinson
Post by Igor Tandetnik
Post by Igor Tandetnik
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.
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.
Post by David Wilkinson
So why exactly do
LPVOID p = LPVOID(n);
LPVOID p(LPVOID(n));
Because these are casts.
Post by David Wilkinson
both work, while
LPVOID p = n;
LPVOID p(n);
do not?
And these are not. LPVOID doesn't have a converting constructor.


Bo Persson
Igor Tandetnik
2010-02-01 16:41:24 UTC
Permalink
Post by David Wilkinson
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;
Post by David Wilkinson
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
Loading...