子类指针转换无效
在以下代码中:
#include "itkImage.h"
#include "itkImageRegionIterator.h"
struct CustomImageBase
{
virtual void DoSomething() = 0;
};
template <typename TPixel>
struct CustomImage : public itk::Image<TPixel, 2>, public CustomImageBase
{
void DoSomething()
{
itk::Index<2> index;
index.Fill(0);
std::cout << this->GetPixel(index);
}
};
int main(int, char *[])
{
std::vector<CustomImageBase*> images;
CustomImage<float>* floatImage = CustomImage<float>::New().GetPointer();
CustomImage<int>* intImage = CustomImage<int>::New().GetPointer();
return EXIT_SUCCESS;
}
我收到错误:从 itk::Image* 到 CustomImage* 的无效转换
请注意,这工作正常:
itk::Image<float>* testFloatImage = itk::Image<float>::New().GetPointer();
由于 CustomImage 继承自 itk::Image,我不明白这个问题?
In the following code:
#include "itkImage.h"
#include "itkImageRegionIterator.h"
struct CustomImageBase
{
virtual void DoSomething() = 0;
};
template <typename TPixel>
struct CustomImage : public itk::Image<TPixel, 2>, public CustomImageBase
{
void DoSomething()
{
itk::Index<2> index;
index.Fill(0);
std::cout << this->GetPixel(index);
}
};
int main(int, char *[])
{
std::vector<CustomImageBase*> images;
CustomImage<float>* floatImage = CustomImage<float>::New().GetPointer();
CustomImage<int>* intImage = CustomImage<int>::New().GetPointer();
return EXIT_SUCCESS;
}
I get the error: invalid convsion from itk::Image* to CustomImage*
Note that this works fine:
itk::Image<float>* testFloatImage = itk::Image<float>::New().GetPointer();
Since CustomImage inherits from itk::Image, I don't understand the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果
B
派生自A
,则无法将A*
转换为B*
。如果C
也派生自A
,并且您的A*
确实指向C
对象而不是>B
对象,如果您假装它是一个B
会发生什么?转换是不安全的。就您而言,您知道GetPointer()
将始终返回CustomImage
。编译器不知道这一点。您可以通过使用强制转换来告诉它:编辑:这正是转换不安全的原因:在阅读有关您问题的评论后,我不相信
CustomImage; ::New().GetPointer()
确实指向CustomImage
,如果不是,则强制转换只会将编译器错误转换为运行时错误。If
B
derives fromA
, you cannot convertA*
toB*
. IfC
also derives fromA
, and yourA*
really points to aC
object instead of aB
object, what would happen if you pretend it is aB
? The conversion is unsafe. In your case, you know thatGetPointer()
will always return aCustomImage<T>
. The compiler does not know that. You can tell it by using a cast:Edit: this is precisely why the conversion is unsafe: after reading the comments on your question, I don't believe
CustomImage<float>::New().GetPointer()
really points to aCustomImage
, and if not, the cast would simply turn compiler errors into runtime errors.由于
CustomImage
继承自itk::Image
,这意味着CustomImage*
可以隐式转换为itk::Image*.然而,编译器抱怨相反转换:
这需要显式类型转换。
我无法编译你的代码,因为我没有先决条件,而且你没有说编译器不喜欢哪一行代码,所以此时很难提供进一步的帮助。
Since
CustomImage
inherits fromitk::Image
, this means thatCustomImage*
can be implicitly converted toitk::Image*
. However, the compiler is complaining about the opposite conversion:This requires an explicit type cast.
I can't compile your code since I don't have the pre-requisites, and you don't say which line of the code the compiler doesn't like, so it's hard to provide further help at this point.
假设这是允许的。现在考虑以下情况:
您可以安全地从指向派生类的指针转换为指向基类的指针,但不能安全地进行相反的转换,因为您不知道所指向的对象是否具有正确的类型。
Suppose this was allowed. Now consider the following:
You can safely convert from a pointer to a derived class to a pointer to base class, but you can't safely convert the other way around, because you don't know if the object pointed to has the correct type.