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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:53:33+00:00 2026-06-18T22:53:33+00:00

I have an interface like this (except much much lengthier than this in the

  • 0

I have an interface like this (except much much lengthier than this in the real library code)

struct IFoo
{
    virtual void onA(A& a) = 0;
    virtual void onB(A& a) = 0;
    virtual void onC(A& a) = 0;
};

and it is common for me to implement different listeners of IFoo. Because of this, I designed a helper class like this one:

template <class T>
struct IFooHelper {
    virtual void onA(A& a) { static_cast<T*>(this)->onGeneric(a); }
    virtual void onB(B& b) { static_cast<T*>(this)->onGeneric(b); }
    virtual void onC(C& c) { static_cast<T*>(this)->onGeneric(c); }
};

so now, when I have a lot of common behavior in a listener, rather than having to provide a virtual override of every single IFoo function, I can do something like:

struct Fox : public IFooHelper<Fox>
{
    template <class T> void onGeneric(T& t) { //do something general }
    void onD(D& d) { //special behavior only for a type D }
};

This has worked fantastically well but now I am implementing a listener for which I want some common behavior, and then update a counter, say, of which type of call it was. In other words, assuming I only have types A,B,C as above, my listener would be:

struct Ugly : public IFooHelper<Ugly>
{
    void onA(A& a) { //8 lines of common code; //update some counter for type A objs; }
    void onB(B& b) { //8 lines of common code; //update some counter for type B objs; }
    void onC(C& c) { //8 lines of common code; //update some counter for type C objs; }
};

In here, the calls have to be very fast (so no lookups) and ideally I would be able to exploit the IFooHelper to hoist the common behavior into the template method, and then somehow still be able to distinguish the types. I was thinking something like a template specialized struct with offsets into a static cons char* array..or with values themselves which are char* depending on T..is there a better way?

  • 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-06-18T22:53:35+00:00Added an answer on June 18, 2026 at 10:53 pm

    Not sure if I fully understand what you are looking for, but I’ll give it a shot. As a first step, consider this:

    struct NotSoUgly : public IFooHelper<NotSoUgly>
    {
        void updateCounter(A& a) { //update some counter for type A objs; }
        void updateCounter(B& b) { //update some counter for type B objs; }
        void updateCounter(C& c) { //update some counter for type C objs; }
    
        template <class T> void onGeneric(T& t) {
            //8 lines of common code;
            updateCounter(t);
        }
    };
    

    Further improvements are possible if you’d show us the content of the updateCounter() methods to we could come up with a single generic implementation for that as well, but without seeing the code, it’s hard to guess.

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

Sidebar

Related Questions

I have an Interface like this: namespace QuickRoutes.Model.Utilities { public interface IRoutesManager { bool
I have an interface like this: public interface IUser{ //some properties here T ToDerived(User
I have an interface and a class implementing the interface like this: public interface
I have a interface member like this. AppInterface.cs ObjLocation[] ArrayLocations { get; } App.cs
I have a method on an interface that looks like this and I want
I have a scene, called testScene, it works like this: @interface testScene : myScene
I have an NSString declared like this : .h: @interface ViewController : UIViewController {
I have a proxied method in a MongoRepository extender class like this: public interface
I have a XIB set up like in this screenshot: alt text http://emberapp.com/jxpx777/images/interface-builder/sizes/m.png File's
In Interface Builder components I have Gradient Button I would like to create this

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.