English 中文(简体)
C++: 固定不变
原标题:C++: can t static_cast from double* to int*

当我试图利用静态的预测,把一 double一 double一 double成一 double一 double,我发现以下错误:

invalid static_cast from type ‘double*’ to type ‘int*’

该守则是:

#include <iostream>
int main()
{
        double* p = new double(2);
        int* r;

        r=static_cast<int*>(p);

        std::cout << *r << std::endl;
}

我的理解是,将双重和隐蔽性转化成问题,但为什么存在着双重和隐蔽之间的转变问题?

最佳回答

除了点名外,<代码>杜布尔*和int*没有什么共同之处。 您可以就<代码>说同样的话。 Foo*和Bar*指任何异构体的点式。

<>code>static_cast means that a pointer of the source category can be used<>em>, as a pointer of the Hawaii category, which requires a subtype relationship.

问题回答

您应使用<代码>reinterpret_cast <>/code>作为投放点人,即:

r = reinterpret_cast<int*>(p);

Of course this makes no sense,

页: 1 你拿到了一些令人ir慕的产出,我认为这并不是你打算做的事。 如欲将 Value 上注明,pint。 然后,

*r = static_cast<int>(*p);

另外,<代码>r不是al place 因此,你可以做以下工作之一:

int *r = new int(0);
*r = static_cast<int>(*p);
std::cout << *r << std::endl;

int r = 0;
r = static_cast<int>(*p);
std::cout << r << std::endl;

表示支持将点对立体转换为int a = 静态_cast<int>5.2)。 然而,这种转换是完全不相容的。 你们再次要求的是,将一个点子转换到一个8级结构,变成一个四级结构的点子,而这种结构是没有任何实际意义的。

尽管如此,如果你真想把你的两倍解释为一种愤怒的话,那么int* r = 重新解释_cast<int*>(p)将做罚款。

您可以翻一番,改写如下:static_cast<>,但不是不同类型。 您可以将任何点类型转换到或从<>代码>中删除*/代码>,static_cast<>

理由可能是<代码>int *和 double *常常是有效的阵容,而实施时确实不知道阵列的大小。

由于您使用<条码>杜布尔*而不是<条码>。

C++不能安全地静态地向不同类型的点人播一个点。

如果你想要做这样的事情,你必须首先放弃变量。

r=new int(static_cast<int>(*p));

页: 1 由于无法在同一记忆空间居住一倍和一辈(平常)





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?