When I try to use a static_cast to cast a double* to an int*, I get the following error:
当我尝试使用static_cast将一个double*转换为int*时,我得到以下错误:
invalid static_cast from type ‘double*’ to type ‘int*’
Here is the code:
这是代码:
#include <iostream>
int main()
{
double* p = new double(2);
int* r;
r=static_cast<int*>(p);
std::cout << *r << std::endl;
}
I understand that there would be problems converting between a double and an int, but why is there a problem converting between a double* and an int*?
我知道在double和int之间转换会有问题,但是为什么在double*和int*之间转换会有问题呢?
5 个解决方案
#1
19
Aside from being pointers, double*
and int*
have nothing in common. You could say the same thing for Foo*
and Bar*
pointer types to any dissimilar structures.
除了是指针之外,double*和int*没有任何共同之处。对于Foo*和Bar*指针类型对于任何不同的结构来说都是一样的。
static_cast
means that a pointer of the source type can be used as a pointer of the destination type, which requires a subtype relationship.
static_cast意味着可以使用源类型的指针作为目标类型的指针,这需要一个子类型关系。
#2
21
You should use reinterpret_cast
for casting pointers, i.e.
您应该对转换指针使用reinterpret_cast,例如。
r = reinterpret_cast<int*>(p);
Of course this makes no sense,
unless you want take a int
-level look at a double
! You'll get some weird output and I don't think this is what you intended. If you want to cast the value pointed to by p
to an int
then,
除非你想要看一个双关!你会得到一些奇怪的输出,我不认为这是你想要的。如果你想将p指向的值转换为int,
*r = static_cast<int>(*p);
Also, r
is not allocated so you can do one of the following:
而且,r没有被分配,所以你可以做以下其中的一个:
int *r = new int(0);
*r = static_cast<int>(*p);
std::cout << *r << std::endl;
Or
或
int r = 0;
r = static_cast<int>(*p);
std::cout << r << std::endl;
#3
4
Floating point-to-integer conversion is supported, so int a = static_cast<int>(5.2)
is fine. However, it's a conversion - the underlying data types are completely incompatible. What you're asking is for the runtime to convert a pointer to an 8-byte structure to a pointer to a 4-byte structure, which it can't do in any meaningful way.
支持浮点到整数的转换,所以int a = static_cast
That having been said, if you really want to interpret your double as an integer, int* r = reinterpret_cast<int*>(p)
will work fine.
话虽如此,如果您真的想将double解释为一个整数,int* r = reinterpret_cast
#4
0
You can convert between a double and an int with static_cast<>
, but not between pointers to different types. You can convert any pointer type to or from void *
with static_cast<>
.
您可以使用static_cast<>在double和int之间进行转换,但不能在指向不同类型的指针之间进行转换。您可以使用static_cast<>将任何指针类型转换为void *。
The rationale may be that int *
and double *
are often effectively arrays, and the implementation doesn't know how big the array is.
理由可能是int *和double *通常是有效的数组,而实现不知道数组的大小。
#5
-1
Because you used double *
instead of double
因为你用了double *而不是double *
The *
after it means that you are declaring a pointer, which is vastly different from a regular double.
后面的*表示您正在声明一个指针,这与普通的double有很大的不同。
C++ can not safely static_cast a pointer to a different type of pointer like that.
c++不能安全地使用static_cast一个指针到另一种类型的指针。
If you are wanting to do this kinda thing, you must first dereference the variable.
如果您想要做这样的事情,您必须首先取消对变量的引用。
r=new int(static_cast<int>(*p));
You must use new
because a double and an integer can not reside in the same memory space(sanely)
必须使用new,因为double和integer不能驻留在相同的内存空间中(明智地)
#1
19
Aside from being pointers, double*
and int*
have nothing in common. You could say the same thing for Foo*
and Bar*
pointer types to any dissimilar structures.
除了是指针之外,double*和int*没有任何共同之处。对于Foo*和Bar*指针类型对于任何不同的结构来说都是一样的。
static_cast
means that a pointer of the source type can be used as a pointer of the destination type, which requires a subtype relationship.
static_cast意味着可以使用源类型的指针作为目标类型的指针,这需要一个子类型关系。
#2
21
You should use reinterpret_cast
for casting pointers, i.e.
您应该对转换指针使用reinterpret_cast,例如。
r = reinterpret_cast<int*>(p);
Of course this makes no sense,
unless you want take a int
-level look at a double
! You'll get some weird output and I don't think this is what you intended. If you want to cast the value pointed to by p
to an int
then,
除非你想要看一个双关!你会得到一些奇怪的输出,我不认为这是你想要的。如果你想将p指向的值转换为int,
*r = static_cast<int>(*p);
Also, r
is not allocated so you can do one of the following:
而且,r没有被分配,所以你可以做以下其中的一个:
int *r = new int(0);
*r = static_cast<int>(*p);
std::cout << *r << std::endl;
Or
或
int r = 0;
r = static_cast<int>(*p);
std::cout << r << std::endl;
#3
4
Floating point-to-integer conversion is supported, so int a = static_cast<int>(5.2)
is fine. However, it's a conversion - the underlying data types are completely incompatible. What you're asking is for the runtime to convert a pointer to an 8-byte structure to a pointer to a 4-byte structure, which it can't do in any meaningful way.
支持浮点到整数的转换,所以int a = static_cast
That having been said, if you really want to interpret your double as an integer, int* r = reinterpret_cast<int*>(p)
will work fine.
话虽如此,如果您真的想将double解释为一个整数,int* r = reinterpret_cast
#4
0
You can convert between a double and an int with static_cast<>
, but not between pointers to different types. You can convert any pointer type to or from void *
with static_cast<>
.
您可以使用static_cast<>在double和int之间进行转换,但不能在指向不同类型的指针之间进行转换。您可以使用static_cast<>将任何指针类型转换为void *。
The rationale may be that int *
and double *
are often effectively arrays, and the implementation doesn't know how big the array is.
理由可能是int *和double *通常是有效的数组,而实现不知道数组的大小。
#5
-1
Because you used double *
instead of double
因为你用了double *而不是double *
The *
after it means that you are declaring a pointer, which is vastly different from a regular double.
后面的*表示您正在声明一个指针,这与普通的double有很大的不同。
C++ can not safely static_cast a pointer to a different type of pointer like that.
c++不能安全地使用static_cast一个指针到另一种类型的指针。
If you are wanting to do this kinda thing, you must first dereference the variable.
如果您想要做这样的事情,您必须首先取消对变量的引用。
r=new int(static_cast<int>(*p));
You must use new
because a double and an integer can not reside in the same memory space(sanely)
必须使用new,因为double和integer不能驻留在相同的内存空间中(明智地)