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 7043387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:16:59+00:00 2026-05-28T02:16:59+00:00

I know that you can store a bunch of Parent* objects in a vector.

  • 0

I know that you can store a bunch of Parent* objects in a vector. However, if the functions you need to call on the objects cannot be defined in the parent class (they depend on a template parameter of the subclass) then how are you supposed to retrieve the objects from the container?

In this example:

#include <iostream>

class ImageBase{};

template <typename TPixel>
class Image : public ImageBase
{
public:
  TPixel GetPixel() const {TPixel a; return a;}
};

template<typename TImage>
void Output(const TImage* image)
{
  std::cout << image->GetPixel();
}

int main(int, char *[])
{
  // Works correctly
  {
  Image<float>* image = new Image<float>;
  image->GetPixel();
  Output(image);
  }

  {
  ImageBase* image = new Image<float>;
  Output(image);
  }

  return 0;
}

The Output(image); where ‘image’ is a ImageBase* fails (of course) because GetPixel is not defined in ImageBase. I know you can dynamic_cast<> to a bunch of types to figure out if the subclass matches any of them, but this list could very quickly get very long. The long list would be fine if it could reside in one place, but how would you make a function to do this? The function would take an ImageBase*, but what would it return?

returnType? GetSubclass(ImageBase* input)
{
  if(dynamic_cast<Image<float>*>(input))
    {
    return Image<float>*;
    }
  else if(dynamic_cast<Image<int>*>(input))
    {
    return Image<int>*;
    }
}

It seems reasonable to me to want to be able to call some template functions on subclasses that only vary in signature by their template parameter (as setup in this example), does it not?

In my real case, both Image and ImageBase are part of a library, so I cannot change them.

  • 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-28T02:16:59+00:00Added an answer on May 28, 2026 at 2:16 am

    Visitor pattern to recover the type information, possibly with a templated helper implementing the visit function.

    First, let’s make your algorithm into a polymorphic functor object:

    struct Output
    {
        std::ostream& dest;
        Output(std::ostream& destination) : dest(destination) {}
    
        template<typename PixelType>
        void operator()(const Image<PixelType>* image) const
        {
            dest << image->GetPixel();
        }
    };
    

    Now, let’s add a visitor interface:

    struct ImageVisitor /* abstract */
    {
        virtual void Visit(Image<RGBQUAD>*) const = 0;
        virtual void Visit(Image<RGBTRIPLE>*) const = 0;
        virtual void Visit(Image<RGBQUAD16>*) const = 0;
        virtual void Visit(Image<RGBTRIPLE16>*) const = 0;
        virtual void Visit(Image<RGBQUADF>*) const = 0;
        virtual void Visit(Image<RGBTRIPLEF>*) const = 0;
        virtual void Visit(Image<RGBQUADD>*) const = 0;
        virtual void Visit(Image<RGBTRIPLED>*) const = 0;
    };
    

    And a forwarder:

    template<typename Functor>
    struct ImageVisitorShim : ImageVisitor
    {
        Functor& fn;
        ImageVisitorShim(Functor& algorithm) : fn(algorithm) {}
    
        virtual void Visit(Image<RGBQUAD>     *im) const { fn(im); }
        virtual void Visit(Image<RGBTRIPLE>   *im) const { fn(im); }
        virtual void Visit(Image<RGBQUAD16>   *im) const { fn(im); }
        virtual void Visit(Image<RGBTRIPLE16> *im) const { fn(im); }
        virtual void Visit(Image<RGBQUADF>    *im) const { fn(im); }
        virtual void Visit(Image<RGBTRIPLEF>  *im) const { fn(im); }
        virtual void Visit(Image<RGBQUADD>    *im) const { fn(im); }
        virtual void Visit(Image<RGBTRIPLED>  *im) const { fn(im); }
    };
    

    And a factory:

    template<typename Functor>
    ImageVisitorShim<Functor> MakeImageVisitor(Functor& f) { return f; }
    

    Now a visitor-compliant image wrapper:

    struct VisitableImageBase
    {
        virtual void VisitWith(const ImageVisitor&) = 0;
    };
    
    template<typename PixelType>
    struct VisitableImage : VisitableImageBase
    {
        unique_ptr<Image<PixelType>> content; // or shared or raw pointer, if ownership is elsewhere
    
        VisitableImage(Image<PixelType>* im) : content(im) {}
    
        virtual void VisitWith(const ImageVisitor& v) { v.Visit(content.get()); }
    };
    

    Finally, you are able to use a polymorphic vector of images!

    vector<unique_ptr<VisitableImageBase>> images;
    Output outputter(std::cout);
    for( auto vim : images ) vim->VisitWith(MakeImageVisitor(outputter));
    

    That was a lot of code, but the good thing is that new types can be added without affecting existing functors (just extend the shim) as long as the functor was implemented with a template. And not much code is needed to add more image processing functions (just a new template functor class, similar to Output).

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

Sidebar

Related Questions

Consider a class Calendar that stores a bunch of Date objects. The calendar is
I know that if I write a class, I can definite a custom print
Possible Duplicate: PHP 2-way encryption: I need to store passwords that can be retrieved
I know that can co-exists with web form no problem as Hanselman mention here
I know that it's a subject that can raise a lot of debate, but
I know that I can do something like $int = (int)99; //(int) has a
I know that you can insert multiple rows at once, is there a way
I know that JTable can sort by a single column. But is it possible
I know that I can share files using Shared Folders in Virtual PC, but
I know that I can insert multiple rows using a single statement, if I

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.