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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:41:30+00:00 2026-06-01T06:41:30+00:00

I have a class: class A { public: virtual void func() {…} virtual void

  • 0

I have a class:

class A 
{
public:
  virtual void func() {…}
  virtual void func2() {…}
};

And some derived classes from this one, lets say B,C,D… In 95 % of the cases, i want to go through all objects and call func or func2(), so therefore i have them in a vector, like:

std::vector<std::shared_ptr<A> > myVec;
…
for (auto it = myVec.begin(); it != myVec.end(); ++it)
  (*it).func();

However, in the rest 5 % of the cases i want to do something different to the classes depending on their subclass. And I mean totally different, like calling functions that takes other parameters or not calling functions at all for some subclasses. I have thought of some options to solve this, none of which I really like:

  • Use dynamic_cast to analyze subclass. Not good, too slow as I make calls very often and on limited hardware

  • Use a flag in each subclass, like an enum {IS_SUBCLASS_B, IS_SUBCLASS_C}. Not good as it doesnt feel OO.

  • Also put the classes in other vectors, each for their specific task. This doesnt feel really OO either, but maybe I’m wrong here. Like:

    std::vector<std::shared_ptr<B> > vecForDoingSpecificOperation;
    std::vector<std::shared_ptr<C> > vecForDoingAnotherSpecificOperation;
    

So, can someone suggest a style/pattern that achieves what I want?

  • 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-01T06:41:31+00:00Added an answer on June 1, 2026 at 6:41 am

    Someone intelligent (unfortunately I forgot who) once said about OOP in C++: The only reason for switch-ing over types (which is what all your suggestions propose) is fear of virtual functions. (That’s para-paraphrasing.) Add virtual functions to your base class which derived classes can override, and you’re set.
    Now, I know there are cases where this is hard or unwieldy. For that we have the visitor pattern.

    There’s cases where one is better, and cases where the other is. Usually, the rule of thumb goes like this:

    • If you have a rather fixed set of operations, but keep adding types, use virtual functions.
      Operations are hard to add to/remove from a big inheritance hierarchy, but new types are easy to add by simply having them override the appropriate virtual functions.

    • If you have a rather fixed set of types, but keep adding operations, use the visitor pattern.
      Adding new types to a large set of visitors is a serious pain in the neck, but adding a new visitor to a fixed set of types is easy.

    (If both change, you’re doomed either way.)

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

Sidebar

Related Questions

I have a question, here are two classes below: class Base{ public: virtual void
Say we have: class Base { virtual void f() {g();}; virtual void g(){//Do some
I have these classes: class Base { public: virtual void foo(int x = 0)
I have 2 classes: class Base { public: virtual int Foo(int n); virtual void
Suppose you have the following situation #include <iostream> class Animal { public: virtual void
I have a ContactModel like this: public class ContactModel { public virtual int ID
I have the following existing classes: class Gaussian { public: virtual Vector get_mean() =
I have a base class: class CBase { public: virtual void SomeChecks() {} CBase()
I have a domain class like this: public class DomainClass { public virtual string
I have a Serializer class like this: class Serializer { public: // Func 1

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.