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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:05:59+00:00 2026-05-14T07:05:59+00:00

There was an interesting problem in C++, but it was more about architecture. There

  • 0

There was an interesting problem in C++, but it was more about architecture.

There are many (10, 20, 40, etc) classes describing some characteristics (mix-in classes), for example:

struct Base { virtual ~Base() {} };

struct A : virtual public Base { int size; };
struct B : virtual public Base { float x, y; };
struct C : virtual public Base { bool some_bool_state; };
struct D : virtual public Base { string str; }
// ....

The primary module declares and exports a function (for simplicity just function declarations without classes):

// .h file
void operate(Base *pBase);

// .cpp file
void operate(Base *pBase)
{
    // ....
}

Any other module can have code like this:

#include "mixing.h"
#include "primary.h"

class obj1_t : public A, public C, public D {};
class obj2_t : public B, public D {};

// ...
void Pass()
{
    obj1_t obj1;
    obj2_t obj2;

    operate(&obj1);
    operate(&obj2);
}

The question is how do you know what the real type of a given object in operate() is without using dynamic_cast and any type information in classes (constants, etc)? The operate() function is used with a big array of objects in small time periods and dynamic_cast is too slow for it and I don’t want to include constants (enum obj_type { ... }) because this is not the OOP-way.

// module operate.cpp

void some_operate(Base *pBase)
{
    processA(pBase);
    processB(pBase);
}

void processA(A *pA)
{
}

void processB(B *pB)
{
}

I cannot directly pass a pBase to these functions. And it’s impossible to have all possible combinations of classes, because I can add new classes just by including new header files.

One solution that came to mind, in the editor I can use a composite container:

struct CompositeObject
{
    vector<Base *pBase> parts;
};

But the editor does not need time optimization and can use dynamic_cast for parts to determine the exact type. In operate() I cannot use this solution.

So, is it possible to avoid using a dynamic_cast and type information to solve this problem? Or maybe I should use another architecture?

  • 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-14T07:06:00+00:00Added an answer on May 14, 2026 at 7:06 am

    The real problem here is about what you are trying to achieve.

    Do you want something like:

    void operate(A-B& ) { operateA(); operateB(); }
    
    // OR
    
    void operate(A-B& ) { operateAB(); }
    

    That is, do you want to apply an operation on each subcomponent (independently), or do you wish to be able to apply operations depending on the combination of components (much harder).

    I’ll take the first approach here.

    1. Virtual ?

    class Base { public: virtual void operate() = 0; };
    
    class A: virtual public Base { public virtual void operate() = 0; };
    void A::operate() { ++size; } // yes, it's possible to define a pure virtual
    
    class obj1_t: public A, public B
    {
    public:
      virtual void operate() { A::operate(); B::operate(); }
    };
    

    Some more work, for sure. Notably I don’t like the repetition much. But that’s one call to the _vtable, so it should be one of the fastest solution!

    2. Composite Pattern

    That would probably be the more natural thing here.

    Note that you can perfectly use a template version of the pattern in C++!

    template <class T1, class T2, class T3>
    class BaseT: public Base, private T1, private T2, private T3
    {
    public:
      void operate() { T1::operate(); T2::operate(); T3::operate(); }
    };
    
    class obj1_t: public BaseT<A,B,C> {};
    

    Advantages:

    • no more need to repeat yourself! write operate once and for all (baring variadic…)
    • only 1 virtual call, no more virtual inheritance, so even more efficient that before
    • A, B and C can be of arbitrary type, they should not inherit from Base at all
    • edit the operate method of A, B and C may be inlined now that it’s not virtual

    Disadvantage:

    Some more work on the framework if you don’t have access to variadic templates yet, but it’s feasible within a couple dozen of lines.

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

Sidebar

Related Questions

Here is an interesting optimization problem that I think about for some days now:
there's this interesting problem i can not solve myself. I will be very glad,
There is an interesting post over here about this, in relation to cross-application flow
Some days ago I looked at boost sources and found interesting typedef . There
I've just solved an interesting problem using MySQL's IN, but I don't know what
I have an interesting problem coming up soon and I've started to think about
This design problem is turning out to be a bit more interesting than I'd
I am implementing this algorithm for a directed graph. But the interesting thing about
What is the string concatenation operator in Oracle SQL? Are there any interesting features
There was an interesting question in a practice test that I did not understand

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.