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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:47:38+00:00 2026-05-23T02:47:38+00:00

It is well-known that STL classes do not use virtual methods anywhere (and STL

  • 0

It is well-known that STL classes do not use virtual methods anywhere (and STL does not use inheritance anywhere, too, and that these two facts are inter-related), and that STL is not unique in that.

Let’s assume that other performance freaks exist on earth [well they exist], such performance freaks who ask themselves for every class “do I need virtual methods for this class X ?” and “can this class X do without any virtuals, just like STL classes, for better performance ?”

Absense of any virtual methods (inclusing d’tor) makes polymorphism and subclassing more difficult than with “virtuals” base classes. Apparently “non-virtuals” classes are not well suited to be base classes.

Question: is there technique (for c++) that allows programmer to create in one shot two versions of same class X, a “non-virtuals” version Xnv (for performance), and “virtuals” version Xv, for subclassing ? If this is not needed, please explain why.

Post-note

People answered
“If you need subclassing, use virtuals. If you don’t, don’t use virtuals”.

There is a problem with this suggestion. Couple of problems.

1) needs undergod changes changes over time. subclassing from class X was not needed then, but is needed now, or vice versa.
2) Person who writes the base class is not the same person who writes the derived class. This is clear from the question. People have different thinking stypes, different judgements, different needs. Clear, again.
3) Hence different programmers, answering question like “does inheriting from class X makes any sense?”, will give different answers. It is subjective, there is no cut-in-stone answer.
4) It contradicts what question asks.

Hence we want to satisfy two ends of the spectrum — which happens often in engineering — and this is motivation behind the question.

The motivation was too complex to express concisely in the question. I assumed people can either (1) assume motivation exists as question was precisely formulated, or can (2) figure motivation because they already were in similar tradeoff-and-balance situation of c++ design.

Nobody figured the motivation — to my surprise — possibly even now. This shall be a lesson for me.

I accepted the answer that mentioned CRTP because it is hilarious pattern.

  • 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-23T02:47:39+00:00Added an answer on May 23, 2026 at 2:47 am

    Question: is there technique (for c++) that allows programmer to create in one shot two versions of same class X, a “non-virtuals” version Xnv (for performance), and “virtuals” version Xv, for subclassing ?

    Why do that, when you can have your cake and eat it too?

    With CRTP, you have compile-time polymorphism, and the ability for subclasses to override behavior, without any overhead for virtual functions.

    Alternatively, you could use a “traits” class to inject behavior.

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

Sidebar

Related Questions

It's well known that Apple does not provide automatic garbage collection on the iPhone
It's quite well known that IE6 does not support direct descendant selection with >
It's well known that Python classes cannot have truly private members. The not- as
I can't seem to find an answer to this anywhere. It's well known that
It is well known that cin is not typesafe (e.g. cin >> integer; and
It's well known that in Windows a directory with too many files will have
It's well known that Silverlight lacks the very compelling x:Type MarkupExtension (MarkupExtension is not
I'm working with a legacy application that makes some use of the well-known/dreaded data
It is well-known that monoids are stunningly ubiquitous in programing. They are so ubiquitous
Its a well known fact that a static method can work only on static

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.