Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

2
  • In C++ one of the main precepts is RAII - so resource allocation is usually done in the constructor (which makes the constructor not atomic). Are you saying that resource allocation shouldn't be done in the constructor? Commented Jul 12, 2021 at 23:47
  • Both can work. In the case of C++ RAII is used frequently. As the object is wrapping the resource, it is both safer then just calling new and probably easier to read too. On the other hand, when using RAII it is usually important to keep in mind, that if the exceptions thrown during resource allocation were not handled properly, the constructor might not have successfully finished (throwing the exception) ... potentially leading to an object that is not "living". That is its destructor might not be called when it goes out of scope, as it never existed. Commented Jul 13, 2021 at 7:42