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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:14:10+00:00 2026-05-19T16:14:10+00:00

I have a base class which implements the following: struct Consumer { template <typename

  • 0

I have a base class which implements the following:

struct Consumer
{
    template <typename T>
    void callback(T msg) { /*null implementation */ }
};

I then have a class implement this:

struct Client : public Consumer
{
     void callback(Msg1 msg);
     void callback(Msg2 msg);
     void callback(Msg3 msg);
};

The issue is I have a container of Client objects treated as Consumer* and I can’t think of a way to get these Consumer objects to call the derived functions. My intended functionality is to have multiple Clients each of which implement an overloaded function for each Msg class that means something to them and the rest of the calls simply call the null implementation in the base class

Any thoughts how I can get the derived class to be called? Right now I need to implement every overloaded function in Consumer and mark them as virtual.

Cheers,
Graeme

  • 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-19T16:14:11+00:00Added an answer on May 19, 2026 at 4:14 pm

    If you really don’t want to use virtual functions (this seems to be a perfect use case for them actually, but I don’t know about your message classes), you can use the CRTP:

    template <typename U>
    struct Consumer
    {
        template <typename T>
        void callback(T msg)
        { static_cast<U*>(this)->callback(msg); }
    };
    
    
    struct Client : Consumer<Client>
    {
         void callback(Msg1 msg);
         void callback(Msg2 msg);
         void callback(Msg3 msg);
    };
    

    The problem, of course, is that you cannot store Consumer objects in a container any more. Since everything is compile time, the actual type of the client must be stored alongside the consumer object for the compiler to call the right callback function. Virtual functions allow you to wait until runtime for this…

    Is there a reason not to have Msg classes polymorphic and use standard virtual functions (other than “I have to rewrite all the code and I cannot”) ?

    EDIT If your concern is about message classes, why not use something like that, assuming message classes implement a DoSomething member function: (this technique is known as Type Erasure)

    struct AnyMsg
    {
        template <typename Msg>
        AnyMsg(Msg x) : impl(newImpl(x)) {}
    
        void DoSomething() { impl->DoSomething(); }
    
    private:
        struct Impl
        {
            virtual ~Impl() {}
            virtual void DoSomething() = 0;
        };
    
        // Probably better is std::unique_ptr if you have
        // C++0x. Or `boost::scoped_ptr`, but you have to
        // provide copy constructors yourself.
        boost::shared_ptr<Impl> impl;
    
        template <typename Msg>
        Impl* newImpl(Msg m)
        {
            class C : public Impl
            {
                void DoSomething() { x.DoSomething(); }
                Msg x;
    
            public:
                C(Msg x) : x(x) {}
            };
    
            return new C(m);
        }
    };
    

    You can customize the behavior of newImpl to get what you want (eg. default actions if there is no DoSomething member function in the message class, specialization for some message classes or anything else). This way, you implement Msg classes like you would have done with your template solution, and you have a unique facade that you can pass to the virtual functions in your client classes.

    If the Message classes are going to be very different, and client classes may react differently to them, and you are going to have a lot of message classes, this begins to smell. Or perhaps you have a candidate for the ugly and scary 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 base class which implements the == operator. I want to write
I have a base class which is non-generic with a derived generic class. The
I have an abstract base class which acts as an interface. I have two
I have a base class in which I want to specify the methods a
I have a base class Node which contains a list of child nodes. Node
I have a base class object array into which I have typecasted many different
I have a base class with a property which (the get method) I want
I have a base class Foo that has an Update() function, which I want
I have an abstract base class called Shape from which both Circle and Rectangle
If I have a table in MySQL which represents a base class, and 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.