Given a class in a any random C++ framework, how do you know if you have to create a simple object on stack or a pointer to an object on heap?
Assume a function which accepts argument in the form of a reference(&) variable. The caller can pass a local variable to it or a pointer too(*ptr). The called function may or may not copy dat from the passed object.
This question is a followup of my previous question related to C++ Bada development.
In general, every well-designed C++ library will allow both. In most cases the library doesn’t care where the caller allocates memory, since the caller handles its own memory. If the library has additional requirements (e.g. that it takes over possession of a pointer) then it will carefully document this.
Furthermore, as a general guideline, avoid pointers and freestore memory. C++ is designed with stack objects in mind. If there is no compelling reason to use something different, then don’t.
newshould be used only very sparingly in your code, anddeleteshould never be used – that is, always let your allocated memory be handled in an automatic fashion (smart pointers).