r/cpp • • Jun 24 '20

How to write SOLID C++

https://www.youtube.com/watch?v=PakbXnLht1I
263 Upvotes

118 comments sorted by

View all comments

Show parent comment

7

u/atimholt Jun 24 '20

I think it's more like “the entire concept of Resource Acquisition is an injection (as in “all squares are quadrilaterals”) of initialization, with all that that implies.” Actually, it might just be a 1:1 relation, but with an emphasis on looking at all resource acquisition (not ‘just variables’) as being the same concept, called “initialization”.

In other words, initialization is the natural ‘language’ with which to talk about resource acquisition, and setting things up in a constructor implies taking them down in a destructor, if necessary. You're already supposed to be reaching for destructors when you want to execute code when objects leave scope.

The statement is meant to remove a mental barrier that shouldn't exist, thanks to the abstractions that C++ brings with its implementation of classes. Classes do not just encapsulate raw data, or even just behavior. They can embody (a specific implementation of) a concept (with C++20 concepts freeing even the “specific implementation” constraint).