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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:51:16+00:00 2026-05-27T17:51:16+00:00

I have been confronted with a C++ design problem to choose between virtual function,

  • 0

I have been confronted with a C++ design problem to choose between virtual function, template and branch. The three implementations are listed as follows. I eventually chose the the second implementation, which looks tricky but with best performance for a low latency design.

virtual function implementation:

class Channel : public BaseChannel
{
  void packet(...) { for (...) message(...); }
  virtual void message(...)=0;
};

class ChannelA : public Channel
{
  struct Header {...}
  void message(...) { ... }
}
class ChannelB : public Channel
{
  struct Header {...}
  void message(...) { ... }
}

template implementation:

template <typename TImpl>
class Channel : public BaseChannel
{
  void packet(...) { for (...) message(...); }
  void message(...);
};

class ChannelA : public Channel<ChannelA>
{
  struct Header {...}
  void message(...) { ... }
}
class ChannelB : public Channel<ChannelB>
{
  struct Header {...}
  void message(...) { ... }
}
template <typename TImpl>
inline void Channel<TImpl>::message(...) { static_cast<TImpl*>(this)->message(); }

branch implementation:

class Channel : public BaseChannel
{
  void packet(...) { for (...) message(...); }
  struct HeaderA {...}
  struct HeaderB {...}
  void message(...)
  {
      if (isHeaderA(...)) messageA(...);
      else if (isHeaderB(...)) messageB(...);
  }
  void messageA(...) { ... }
  void messageB(...) { ... }
};
  • 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-27T17:51:17+00:00Added an answer on May 27, 2026 at 5:51 pm

    What dose your expected?

    1. The Polymorphism – Easy to extends the objects, and support varies object, and you will not meet the parabolic-if statement, but you need to do the more coding and design, and it is hard to understand for one who doesn’t strong with the OO concept or the Design Pattern concept.

    2. The Template – This seem likely the branch implementation in (3), because you need to store an object individually.

    3. The Branch – Basic and easy to code, but this will be a pain when you extends those objects, for example, how the code should be look like when you have OjbectA-ObjcetJ. Well, it is the parabolic-id 🙂

    Both (1) and (3) need the runtime performance overhead, and (1) have the better performance than(3) ,as I have done the experiments. (2) will make your program bigger and it is like the (3).

    In conclusion, you can choose (1) if you have varies of your derived classes or it may increase in the future, or you want your performance, but you can choose (3) if you doesn’t have much object to derived, or you don’t need a good performance ,or you don’t wan’t to do more coding.

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

Sidebar

Related Questions

Have been trying to fix this problem I have here. Basically I have a
I have been asked to develop a highly secure B2B File Transfer system between
I have been looking for a solution to this problem for a while now
Have been digging around the docs but cannot find if there is a function
Have been looking at the MVC storefront and see that IQueryable is returned from
Have been studying the file system related classes of Adobe AIR 1.5, but so
Have been struggling with Javascript closure for a while trying to wrap brain around
We have been using CruiseControl for quite a while with NUnit and NAnt. For
I have been experimenting with woopra.com A web analytics tool. Which requires a piece
I have been working on a web services related project for about the last

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.