Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7056309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:51:07+00:00 2026-05-28T03:51:07+00:00

In the following code: #include itkImage.h #include itkImageRegionIterator.h struct CustomImageBase { virtual void DoSomething()

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:51:08+00:00Added an answer on May 28, 2026 at 3:51 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T
In the following code: #include <iostream> template <typename T, size_t N> void cal_size(T (&a)[N])
Please consider the following code: #include <iostream> #include <typeinfo> template< typename Type > void
I have the following code: #include <iostream> class Grandma { public: virtual void foo()
The following code: #include <stdio.h> class Parent { public: virtual void func() {printf(Parent\n);} };
The following code #include <stdio.h> template <typename T, T v> class Tem { T
I have the following code: #include <cstdio> template<class Fun, class... Args> void foo(Fun f,
I have the following code #include <iostream> #include <vector> class Entity { public: virtual
Consider the following code: #include <iostream> struct X{ X(){ throw 0; } }; void
Consider the following code: #include <iostream> template<class T> struct outer { struct inner {};

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.