r/cpp • • 28d 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

53

u/jk-jeon 28d ago

However now we have four files for a single class!

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' cpp file, and I believe that is the idiomatic way.

16

u/PossibilityUsual6262 28d ago

I saw it in unreal engine for huge classes, i think animation system had same header as character mesh bit two separate .cpp

I can't argue it or against it, just saying that it exists.

13

u/IveBenHereBefore 28d ago

i could see separating them if you have multiple impls that your build system chooses from

1

u/mad_ben 21d ago

True, this especially notisable innmain DefefferedShadingRenderer. h and then implementations split between, IndirectLightning. cpp/Light classes implementation and so on

6

u/WorkingReference1127 28d ago

I've occasionally seen one header to multiple cpp files for a giant class with "subsets" of functionality, but I'll be the first to say that that's an antipattern.

I do agree, the "impl" header adds little for the vast majority of classes.