Skip to main content
32 votes
Accepted

Never make public members virtual/abstract - really?

Saying that it is an anti-pattern to make public methods virtual or abstract because of the developer of a derived class that implements Method1 and overrides Method2 has to repeat the argument ...
Doc Brown's user avatar
  • 221k
10 votes
Accepted

C++ : What is the order of function pointers inside vtable?

While it is implementation dependent, it is also fairly predictable.  Especially historically speaking, the compilers layout members in declaration order.  For fields (instance data) that ...
Erik Eidt's user avatar
  • 34.8k
9 votes

C++ : What is the order of function pointers inside vtable?

It's all up to the implementation. There is no requirement that virtual functions be implemented by a per-class vtable, a per-object vtable, or any other mechanism. An implementation can choose to be ...
Caleth's user avatar
  • 12.4k
7 votes

Do any compilers do this optimization for virtual calls?

I believe the term you're looking for is "devirtualization". Anyway, did you try it? If we put that example in Compiler Explorer: extern void extCall (); class A { public: virtual void Foo() ...
Alex Celeste's user avatar
6 votes

Never make public members virtual/abstract - really?

Doing it the way your colleague suggests does provide more flexibility to the implementor of the base class. But with it also comes more complexity which is typically not justified by the presumed ...
Martin Maat's user avatar
  • 18.6k
5 votes

True cost of virtual dispatch in C++ - when stop using it?

Sure, virtual calls have some runtime overhead. But when you use virtual functions, it's usually not a choice but a consequence of the problem you are solving. If you need to call a function on a type-...
amon's user avatar
  • 136k
4 votes
Accepted

Differences between branching and virtual methods

The C++ compiler creates the vtable for each class. If classes A and B both are sub-classes of class Base, and override methods in base, then the vtable for A and the vtable for B are constructed with ...
BobDalgleish's user avatar
  • 4,749
4 votes

Never make public members virtual/abstract - really?

In C++, this is called the non-virtual interface pattern (NVI). (Once upon a time, it was called the Template Method. That was confusing, but some of the older articles have that terminology.) NVI ...
Adrian McCarthy's user avatar
3 votes

Help with optimizing virtual method

If you always have access to the fully-derived type you are using, you don't need virtual functions at all. struct BaseReducer{ std::size_t result = 0; }; // CountReducer and SumReducer as before,...
Caleth's user avatar
  • 12.4k
3 votes

Why do we need factories in the first place?

The real question is what you want to achieve, and either achieve it in C++, or in another language if you think it would suit the needs better. Why can't virtual constructors exist in C++? Imagine ...
Christophe's user avatar
  • 82.3k
3 votes
Accepted

Object Oriented Programming - what is the best way to add new parameters?

What I was wondering is whether there is a natural OOP way to face this kind of problem. There is. Separate use from construction. However this break the Liskov substitution principle when calling ...
candied_orange's user avatar
3 votes

True cost of virtual dispatch in C++ - when stop using it?

You suggest to stop using virtual functions at some point, but don't suggest an alternative, so that makes the question of "at what point to stop" hard to answer. Virtual functions are not ...
Erik Eidt's user avatar
  • 34.8k
3 votes
Accepted

How to define values dependant on the derived type?

You use what is more robust and less error prone. Should you be told that the performance of this particular function is important, and after you have profiled it, you can change it. I very very much ...
gnasher729's user avatar
  • 49.4k
2 votes

Do any compilers do this optimization for virtual calls?

It's a good guess, but not necessarily true, that when this->ManyFoo() called the implementation inside A, the implementation of this->Foo() will also be the one inside A. So the compiler could ...
gnasher729's user avatar
  • 49.4k
2 votes

Should a base class implement a virtual method for the most common type or derived class?

I would only implement the method in the base class if it is an acceptable default for all subclasses (both currently known and future). A virtual method is a promise of being able to perform some ...
Becuzz's user avatar
  • 4,865
2 votes

Never make public members virtual/abstract - really?

tl;dr– Argument-validation is a run-time check for the part of the method-signature that isn't checked at compile-time. Prohibiting public abstract/virtual methods is a pattern for ensuring that the ...
Nat's user avatar
  • 1,101
1 vote

Why do we need factories in the first place?

In OO programming, you have a factory in order to construct objects - it's really that simple. Most languages have a "new" keyword, which is a shortcut for a compiler-defined static method ...
Steve's user avatar
  • 12.7k
1 vote

True cost of virtual dispatch in C++ - when stop using it?

You should stop using virtual calls if: This leads to a measurable speed improvement. For in-house software: Cost savings due to the speed improvement demonstrably outweigh the development cost. ...
gnasher729's user avatar
  • 49.4k
1 vote

Should a base class implement a virtual method for the most common type or derived class?

Yes, of course. That is exactly the point of abstract classes. An abstract class allows you to define some 'default' behavior for all subclasses. The alternative, with no default behavior, is an ...
whatsisname's user avatar
  • 27.7k
1 vote

Should a base class implement a virtual method for the most common type or derived class?

If the methods pertain to the real reason the class exists, your approach is fine. If the methods are just utility functions and inheritance is being used as a means to share those utilities, you ...
Greg Burghardt's user avatar
1 vote

Should a base class implement a virtual method for the most common type or derived class?

Seems fine to me in principle, but it depends exactly what code is shared, why it is being shared, and how this might change if you add a DerivedClassD, DerivedClassE, etc. If you have an ...
Sean Burton's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible