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

  • SEARCH
  • Home
  • 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 3361252
In Process

The Archive Base Latest Questions

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

I’ve stumbled into a problem which, at a glance, is easy cplusplusable, but on

  • 0

I’ve stumbled into a problem which, at a glance, is easy cplusplusable, but on careful study appears not to be.

I have items in my program which I will need to work with many types, so I have decided to handle these in a generic, extensible way:

class ItemBase
{
public:
 virtual ~ItemBase() = 0 {}
    // pure virtual class, some extra members
};

template<typename T>
class ItemT : public ItemBase
{
public:
 ItemT(const T &data) : m_Data(data) {}

 T m_Data;
};

I can now store any type in a collection:

std::vector<ItemBase*> items;

This is fine. Now I have GUI components I want to keep separate from this class so I want to generate the GUI components depending on type:

GuiComponent* BuildComponent(ItemT<int> &item)
{
    // do whatever based on this type, the int is needed
}

GuiComponent* BuildComponent(ItemT<double> &item)
{
    // do whatever based on this type, the double is needed
}

Which is almost beautiful coding. Unfortunately it does not work. As this program shows:

std::vector<ItemBase*> m_Items;
m_Items.push_back(new ItemT<int>(3));
m_Items.push_back(new ItemT<double>(2.0));
BuildComponent(*m_Items[0]);

Because m_Items[0] is of type ItemBase*.

So how do I solve this problem? What design pattern or template trickery can help me out here?

  • 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-18T03:07:49+00:00Added an answer on May 18, 2026 at 3:07 am

    The easy answer would be : add a virtual buildComponent method in ItemBase. However, as you want to keep the GUI Components separated, I’d recommend a Visitor pattern.

    A very simple implementation would consist of :

    • Adding a single accept(AbstractVisitorType &) virtual method in ItemBase
    • Implementing this method in every derived class by calling visitor.visit(*this), which means that the AbstractVisitorType must provide a virtual visit method for every concrete type it might be called with (see note below)
    • Provide a concrete implementation of this visitor which will instantiate the appropriate GUI object in each of its visit overload based on the parameter type.

    Note however that the Visitor pattern is only appropriate for a reasonably stable class hierarchy : using a new instantiation of the ItemT template will require maintenance on the Visitor side to handle this new type (but your initial idea had the same issue).

    Chapter 10 of Modern C++ Design (Andrei Alexandrescu) is a great read regarding the visitor pattern.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.