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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:52:32+00:00 2026-05-14T21:52:32+00:00

I feel like the answer to this question is really simple, but I really

  • 0

I feel like the answer to this question is really simple, but I really am having trouble finding it. So here goes:

Suppose you have the following classes:

class Base;
class Child : public Base;

class Displayer
{
public:
    Displayer(Base* element);
    Displayer(Child* element);
}

Additionally, I have a Base* object which might point to either an instance of the class Base or an instance of the class Child.

Now I want to create a Displayer based on the element pointed to by object, however, I want to pick the right version of the constructor. As I currently have it, this would accomplish just that (I am being a bit fuzzy with my C++ here, but I think this the clearest way)

object->createDisplayer();

virtual void Base::createDisplayer()
{
     new Displayer(this);
}

virtual void Child::createDisplayer()
{
     new Displayer(this);
}

This works, however, there is a problem with this:

Base and Child are part of the application system, while Displayer is part of the GUI system. I want to build the GUI system independently of the Application system, so that it is easy to replace the GUI. This means that Base and Child should not know about Displayer. However, I do not know how I can achieve this without letting the Application classes know about the GUI.

Am I missing something very obvious or am I trying something that is not possible?

Edit: I missed a part of the problem in my original question. This is all happening quite deep in the GUI code, providing functionality that is unique to this one GUI. This means that I want the Base and Child classes not to know about the call at all – not just hide from them to what the call is

  • 1 1 Answer
  • 1 View
  • 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-14T21:52:32+00:00Added an answer on May 14, 2026 at 9:52 pm

    It seems a classic scenario for double dispatch. The only way to avoid the double dispatch is switching over types (if( typeid(*object) == typeid(base) ) ...) which you should avoid.

    What you can do is to make the callback mechanism generic, so that the application doesn’t have to know of the GUI:

    class app_callback {
      public:
        // sprinkle const where appropriate...
        virtual void call(base&)    = 0;
        virtual void call(derived&) = 0;
    };
    
    class Base {
      public:
        virtual void call_me_back(app_callback& cb) {cb.call(*this);}
    };
    class Child : public Base {
      public:
        virtual void call_me_back(app_callback& cb) {cb.call(*this);}
    };
    

    You could then use this machinery like this:

    class display_callback : public app_callback {
      public:
        // sprinkle const where appropriate...
        virtual void call(base&    obj) { displayer = new Displayer(obj); }
        virtual void call(derived& obj) { displayer = new Displayer(obj); }
    
        Displayer* displayer;
    };
    
    Displayer* create_displayer(Base& obj)
    {
      display_callback dcb;
      obj.call_me_back(dcb);
      return dcb.displayer;
    }
    

    You will have to have one app_callback::call() function for each class in the hierarchy and you will have to add one to each callback every time you add a class to the hierarchy.
    Since in your case calling with just a base& is possible, too, the compiler won’t throw an error when you forget to overload one of these functions in a callback class. It will simply call the one taking a base&. That’s bad.

    If you want, you could move the identical code of call_me_back() for each class into a privately inherited class template using the CRTP. But if you just have half a dozen classes it doesn’t really add all that much clarity and it requires readers to understand the CRTP.

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

Sidebar

Related Questions

I feel like this question has a really easy answer and I'm just overlooking
I feel like this question must have been asked before but I must not
I feel like I've only ever seen this here on SO, but I can't
I feel like there's a pretty simple way to do this, but I'm not
I feel like there must be something really simple that I'm not seeing here.
I have a rather simple question for you.. I feel like I should have
I feel like this question is basic enough to be out there somewhere, but
Disclaimer I feel like this is a fairly simple question, so i must reiterate
I am flying blind on scheme, and I feel like once I answer this
I feel like such a noob asking this but, for some reason a horizontal

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.