子类指针转换无效

发布于 2024-12-26 04:29:04 字数 905 浏览 0 评论 0原文

在以下代码中:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬﹪忆之殇 2025-01-02 04:29:04

如果 B 派生自 A,则无法将 A* 转换为 B*。如果 C 也派生自 A,并且您的 A* 确实指向 C 对象而不是 >B 对象,如果您假装它是一个 B 会发生什么?转换是不安全的。就您而言,您知道 GetPointer() 将始终返回 CustomImage。编译器不知道这一点。您可以通过使用强制转换来告诉它:

CustomImage<float>* floatImage = static_cast<CustomImage<float>*>(CustomImage<float>::New().GetPointer());

编辑:这正是转换不安全的原因:在阅读有关您问题的评论后,我不相信CustomImage; ::New().GetPointer() 确实指向 CustomImage,如果不是,则强制转换只会将编译器错误转换为运行时错误。

If B derives from A, you cannot convert A* to B*. If C also derives from A, and your A* really points to a C object instead of a B object, what would happen if you pretend it is a B? The conversion is unsafe. In your case, you know that GetPointer() will always return a CustomImage<T>. The compiler does not know that. You can tell it by using a cast:

CustomImage<float>* floatImage = static_cast<CustomImage<float>*>(CustomImage<float>::New().GetPointer());

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 a CustomImage, and if not, the cast would simply turn compiler errors into runtime errors.

窗影残 2025-01-02 04:29:04

既然CustomImage继承自itk::Image,我不明白这个问题?

由于 CustomImage 继承自 itk::Image,这意味着 CustomImage* 可以隐式转换为 itk::Image*.然而,编译器抱怨相反转换:

无效转换 itk::Image* CustomImage*

这需要显式类型转换。

我无法编译你的代码,因为我没有先决条件,而且你没有说编译器不喜欢哪一行代码,所以此时很难提供进一步的帮助。

Since CustomImage inherits from itk::Image, I don't understand the problem?

Since CustomImage inherits from itk::Image, this means that CustomImage* can be implicitly converted to itk::Image*. However, the compiler is complaining about the opposite conversion:

invalid convesion from itk::Image* to CustomImage*

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.

拔了角的鹿 2025-01-02 04:29:04

假设这是允许的。现在考虑以下情况:

struct yet_another_image : public itk::Image<TPixel, 2> {
}

yet_another_image img;
itk::Image<TPixel, 2>* ptr = &img; // ok
CustomImage* bad = ptr; // oops!

您可以安全地从指向派生类的指针转换为指向基类的指针,但不能安全地进行相反的转换,因为您不知道所指向的对象是否具有正确的类型。

Suppose this was allowed. Now consider the following:

struct yet_another_image : public itk::Image<TPixel, 2> {
}

yet_another_image img;
itk::Image<TPixel, 2>* ptr = &img; // ok
CustomImage* bad = ptr; // oops!

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文