3 ms·
Yes Kenji... You put new/delete in constructor/destructor so that you don't have to call new/delete manually.
by saynsedit 10y ago
Yes Kenji... You put new/delete in constructor/destructor so that you don't have to call new/delete manually.
- Kenji 10y agoOh, I misunderstood you. I thought you would write code without a single new or delete at all. So, everything is either living on the stack or using built-in allocations like vector and the like.
- slededit 10y agoIn modern C++ you do avoid it completely. Using things like std::make_unique<> and std::make_shared<> to allocate an object and stuff it into a smart pointer immediately.
- Kenji 10y agoThanks. I always wanted to learn modern C++, but there was no such lecture at uni and in my spare time I was only able to fill the gaps partially, learning about the obvious things like the auto keyword and move constructors and the like.
- slededit 10y agoMove constructors get a lot of press because they are new (and necessary for the underlying machinery of unique_ptr to work). But most people will never need to use it or know about it directly. There are really two classes of C++ features, the basic use part which is fairly straightforward and nice to use. This is the API provided by the STL. Followed by the infrastructure stuff like templates, move semantics, SFINAE and other messy and non-obvious things. This latter part is much more complicated and still a mine field - but necessary for the STL to do what it does. If you want to learn C++ get proficient in using the STL. The rest should only be learned once that is second nature.