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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:16:50+00:00 2026-05-17T15:16:50+00:00

I have a design where I have a std::list of base pointers that I’d

  • 0

I have a design where I have a std::list of base pointers that I’d like to transform into a parallel list that adds behavior. The problem I’m having is that the object that I’m trying to use to do the transform doesnt know what the actual types are when it is invoked.

It’s quite possible that I’m missing something subtle and that there is an easy fix. However, if this is a design flaw (I’ve seen this suggested in other posts) what is the appropriate way to approach this?

Suppose the following:

class Sprite { /* ... */ };
class Character : public Sprite {};
class Markup : public Sprite {};

These are constructed (based on some input) into a std::list< Sprite * >. What I’d like to do is eventually take the list and transform it into a parallel structure suitable for output operations. For instance, given:

class HTMLSprite { /* ... */ };
class HTMLCharacter : public HTMLSprite {};
class HTMLMarkup : public HTMLSprite {};

I’d ideally like to do something like

std::transform(sprites.begin (), sprites.end (), html.begin (), HTMLConvert);

with something like

struct HTMLConvert_ {
  HTMLSprite * operator () (const Character * c) { return new HTMLCharacter (); }
  HTMLSprite * operator () (const Markup * c) { return new HTMLMarkup (); }
} HTMLConvert;

Now, I get the error

call of `(HTMLConvert) (const Sprite* const&)' is ambiguous
HTMLSprite* HTMLConvert::operator()(const Character*) const <near match>

Which leads me to my question. What is the best solution to this problem – redesign or otherwise?

Thanks.

  • 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-17T15:16:50+00:00Added an answer on May 17, 2026 at 3:16 pm

    In addition to JoshD’s suggestion, you can keep the door open for other transformations by using the visitor pattern.

    Add a method dispatch_visit to the Sprite hierarchy, using covariant return types:

    class Sprite{
        virtual HTMLSprite * dispatch_visit( HTMLConvert_ const &c ) const = 0;
    };
    class Character : public Sprite {
        virtual HTMLCharacter * dispatch_visit( HTMLConvert_ const &c ) const
            { return c( this ); }
    };
    class Markup : public Sprite {
        virtual HTMLMarkup * dispatch_visit( HTMLConvert_ const &c ) const
            { return c( this ); }
    };
    

    This allows each object to notify the converter of its dynamic type — essentially, a dynamic dispatch into the parallel type, or even a parallel type hierarchy. Everything else pretty much works as your code is written… the best candidate is chosen among the operator()() functions of the converter, based on the static parameter type.

    Oh, you’ll need to add the “missing function” to the converter:

    struct HTMLConvert_ {
      HTMLSprite * operator () (const Sprite * c) { return c->dispatch_visit( *this ); }
      HTMLCharacter * operator () (const Character * c) { return new HTMLCharacter (); }
      HTMLMarkup * operator () (const Markup * c) { return new HTMLMarkup (); }
    } HTMLConvert;
    

    Hmm, that repetitive function could be encapsulated by a template… this only seems to work in C++0x if you want the visitable template to automatically determine the return type of dispatch_visit. If you don’t like principal_base you can factor it out.

    #include <functional>
    
    template< class T >
    struct principal_base
        { typedef void type; };
    
    template< class Client, class Visitor,
        class Base = typename principal_base< Client >::type >
    struct visitable :
        virtual visitable< typename principal_base< Client >::type, Visitor > {
        virtual typename std::result_of< Visitor( Client * ) >::type
        dispatch_visit( Visitor const &v ) const
            { return v( static_cast< Client const * >( this ) ); }
    };
    
    template< class Client, class Visitor >
    struct visitable< Client, Visitor, void > {
        virtual typename std::result_of< Visitor( Client * ) >::type
        dispatch_visit( Visitor const &v ) const = 0;
    };
    
    class HTMLSprite { /* ... */ };
    class HTMLCharacter : public HTMLSprite {};
    class HTMLMarkup : public HTMLSprite {};
    
    class Sprite;
    class Character;
    class Markup;
    
    struct HTMLConvert_ {
      HTMLSprite * operator () (const Sprite * c);
      HTMLCharacter * operator () (const Character * c);
      HTMLMarkup * operator () (const Markup * c);
    } HTMLConvert;
    
    class Sprite : public visitable< Sprite, HTMLConvert_ > {};
    template<> struct principal_base< Character >
        { typedef Sprite type; };
    class Character : public Sprite, visitable< Character, HTMLConvert_ > {};
    template<> struct principal_base< Markup >
        { typedef Sprite type; };
    class Markup : public Sprite, visitable< Markup, HTMLConvert_ > {};
    
    //class Invalid : Character, Markup {};
    
    HTMLSprite * HTMLConvert_::operator () (const Sprite * c)
        { return c->dispatch_visit( *this ); }
    HTMLCharacter * HTMLConvert_::operator () (const Character * c)
        { return new HTMLCharacter (); }
    HTMLMarkup * HTMLConvert_::operator () (const Markup * c)
        { return new HTMLMarkup (); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project that adds elements to an AutoCad drawing. I noticed that
I have a script that appends some rows to a table. One of the
I have a new web app that is packaged as a WAR as part
I have a snippet to create a 'Like' button for our news site: <iframe
I have found this example on StackOverflow: var people = new List<Person> { new
I have several USB mass storage flash drives connected to a Ubuntu Linux computer
I have a login.jsp page which contains a login form. Once logged in the
i have a input tag which is non editable, but some times i need
Let say I have the following desire, to simplify the IConvertible's to allow me
I want to have generalised email templates. Currently I have multiple email templates with

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.