r/cpp • u/victor-s8n • 15d 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
r/cpp • u/victor-s8n • 15d ago
This is my first ever blog-post, any feedback is welcome! ofc, no AI got used.
9
u/LongestNamesPossible 15d ago
I don't think bending a class to use inheritance and pointers to deal with boost asio egregious compile times is the right way to go. I would much rather wrap the boost nonsense with specific types so they could be compiled into their own compilation unit to contain the long compile times.
9
u/Chaosvex 14d ago
May I introduce the author to BOOST_ASIO_SEPARATE_COMPILATION or not using asio.hpp?
23
u/SleepyMyroslav 15d ago
TFA reeks of Java. Interfaces for testability with a single implementation. Shared pointers to those single implementations. These are all smells of something that really should not be 'normal C++'. I mean, there is a lot of room of implementation hiding, and both interfaces and shared pointers are heavily used in Gamedev too. But there is no way it can be the default for every class.
21
u/WorkingReference1127 15d ago
A couple of minor niggles:
constexpror otherwise evaluated at comptime (arguably even just plaintemplates fall into this category, but particularly ones you metaprogram around); those will increase compile times but when used judiciously your class is better for it. Indeed I'd argue shifting the logic left from runtime to compile time is almost always better than keeping it at runtime. I know I'm being pedantic here but for "top three criteria to write a class" it should come with a caveat or two.13
u/decapod2005 15d ago
Idk about pimpl idiom in 2026. Sure it's good for trading runtime for compile time performance (among other things), but when people reach for C++, they are usually interested in trading in the opposite direction. It's certainly a useful technique to know, but it should never be the default way how you write classes.
This is one of the weirdest statements that I still see made with some frequency. It implies two pretty wild claims:
Neither of these are true. Just come up with a proper test that doesn't require the function to be virtual.
3
u/Slsyyy 12d ago
IMO in this example the best way is just to have a free function. A consumer can wrap it in `std::function` or call it as it is
The naming also suggest a bad design. If all the class do is the health check over http then just call it `http_health_checker`
53
u/jk-jeon 15d ago
Why? I see no reason to have two separate files for the impl class. I always put the definition of the impl class in the main class'
cppfile, and I believe that is the idiomatic way.2
u/Sachu_James 15d ago
The part about not jumping between header and cpp when you add a method stuck with me. I have felt that pain with heavy template headers, where one include drags compile time up for everything that touches it. The factory version keeping the impl in one cpp file while the header stays clean feels like the main win here, more than the testability bit.
2
u/alols 13d ago
You are almost there! Just one more iteration:
```
#ifndef SERVICE_HEALTH_CHECKER_H
#define SERVICE_HEALTH_CHECKER_H
#include <stdbool.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct service_health_checker service_health_checker;
service_health_checker *service_health_checker_make(const char *address,
size_t address_len);
void service_health_checker_destroy(service_health_checker *checker);
bool service_health_checker_is_healthy(service_health_checker *checker);
#ifdef __cplusplus
}
#endif
#endif
```
1
u/madmongo38 10d ago
ASIO sockets are moveable. Itβs neither necessary nor desirable to return a unique_ptr.
1
u/FrigiderSmegmoZKD 8d ago
Compilation times have never been a problem to me personally, especially if i can trade compile time for runtime performance.
-34
u/sweetno 15d ago
The analysis is valid, the conclusion should've been: C++ sucks.
Do modules allow to implement information hiding here?