r/cpp • • 26d ago

How I design C++ classes

https://rafb.eu/blog/cpp-class-design/

This is my first ever blog-post, any feedback is welcome! ofc, no AI got used.

66 Upvotes

46 comments sorted by

View all comments

Show parent comment

2

u/victor-s8n 22d ago

You can set the impl methonds to `final` https://en.cppreference.com/cpp/language/final . In doing so the compiler can reason better about the v-table.

Over the past years I learned that 95% of the code is not executed very often. There is a 5% hotpath, but you, know, measure first. For the other 95% of the code, it's about maintainability, testablilty, and yes, compilation-time.

yes "Non-virtual functions are testable".

It's C++.

EVERYTHING is possible, which leads to projects being unmaintainable. I think this makes C++ unpopular. This thinking of the few nanoseconds that one can strip off, by optimizing a `virtual`. Code that only runs once when eg parsing a config-file.

1

u/decapod2005 22d ago

final keyword only helps when you are not actually doing polymorphism. As long as you access the impl function through the base class interface, the compiler can do nothing.

Measuring is important to find bottlenecks, but they become buch harder to find when your first, default approach is already pessimized. When every functionality has to go through hundreds of virtual function calls, you won't see any improvement if you optimize one of them.

It's not a good idea to by default add abstractions that you do not need.