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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:43:12+00:00 2026-05-14T08:43:12+00:00

A scene like this: I’ve different of objects do the similar operation as respective

  • 0

A scene like this:
I’ve different of objects do the similar operation as respective func() implements.
There’re 2 kinds of solution for func_manager() to call func() according to different objects

Solution 1: Use virtual function character specified in c++. func_manager works differently accroding to different object point pass in.

class Object{
  virtual void func() = 0;
}
class Object_A : public Object{
  void func() {};
}
class Object_B : public Object{
  void func() {};
}
void func_manager(Object* a)
{
   a->func();
}

Solution 2: Use plain switch/case. func_manager works differently accroding to different type pass in

typedef enum _type_t
{
  TYPE_A,
  TYPE_B
}type_t;

void func_by_a()
{
// do as func() in Object_A
}
void func_by_b()
{
// do as func() in Object_A
}
void func_manager(type_t type)
{
    switch(type){
        case TYPE_A:
            func_by_a();
            break;
        case TYPE_B:
            func_by_b();
        default:
            break;
    }
}

My Question are 2:
1. at the view point of DESIGN PATTERN, which one is better?
2. at the view point of RUNTIME EFFCIENCE, which one is better? Especailly as the kinds of Object increases, may be up to 10-15 total, which one’s overhead oversteps the other? I don’t know how switch/case implements innerly, just a bunch of if/else?

Thanks very much!

  • 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-14T08:43:13+00:00Added an answer on May 14, 2026 at 8:43 am

    from the view point of DESIGN PATTERN, which one is better?

    Using polymorphism (Solution 1) is better.
    Just one data point: Imagine you have a huge system built around either of the two and then suddenly comes the requirement to add another type. With solution one, you add one derived class, make sure it’s instantiated where required, and you’re done. With solution 2 you have thousands of switch statements smeared all over the system and it is more or less impossible to guarantee you found all the places where you have to modify them for the new type.

    from the view point of RUNTIME EFFCIENCE, which one is better? Especailly as the kinds of Object

    That’s hard to say.
    I remember a footnote in Stanley Lippmann’s Inside the C++ Object Model, where he says that studies have shown that virtual functions might have a small advantage against switches over types. I would be hard-pressed, however, to cite chapter and verse, and, IIRC, the advantage didn’t seem big enough to make the decision dependent on it.

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

Sidebar

Related Questions

I have a scene, called testScene, it works like this: @interface testScene : myScene
I am trying to assemble a scene in opengl, using already made objects. The
Is there a list of 3D cards available that provide full scene antialiasing as
I have a program in which the user adds multiple objects to a scene.
I would like to implement something like this because my application is divided into
The scroller and other widgets in Palm's WebOS are commonly called like so: this.controller.setupWidget(Mojo.Menu.appMenu,
I can't figure out what is up with this. I have a Scene class
I do see tutorials like this one http://www.youtube.com/watch?v=8X-vDtEwpeo They're good but they just describe
I'm looking to create a scene-graph for my F# project somthing like: root ->player_bob
I'd like to learn how FireFox works behind the scenes. I'd like to 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.