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 ...
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 ...
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 ...
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() ...
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 ...
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-...
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 ...
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 ...
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,...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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.
...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
virtual-functions × 23c++ × 16
object-oriented × 9
inheritance × 3
object-oriented-design × 2
constructors × 2
abstract-class × 2
design × 1
java × 1
c# × 1
unit-testing × 1
terminology × 1
naming × 1
language-design × 1
optimization × 1
functions × 1
class × 1
branching × 1
mocking × 1
syntax × 1
null × 1
collections × 1
liskov-substitution × 1
object × 1
keywords × 1