The two are very different things.
QString str("my string");
creates an object whose lifetime is automatically managed: The object either lives to the end of its enclosing scope (if it is a local variable), or to the end of the program (if it is a global variable).
new QString("my string");
creates an object with manually managed lifetime, also called a "dynamic object" (and returns a pointer to this object). This means that you are responsible for managing the object's lifetime. This is almost never the right thing to do, unless you are writing a library component.
And here lies the heart of the C++ philosophy: C++ is a language for library writing. You have the tools to write high-quality, reusable components. If and when you do this, you will need to know the intricacies of lifetime management. However, until such time come, you should use existing library components. When doing so, you will find that you will almost never need to perform any manual management at all.
Use dynamic containers (vectors, strings, maps, etc.) to store data and build your own data structures. Pass arguments by reference if you need to modify objects in the caller's scope. Build complex classes from simpler components. If you really must have dynamic objects, handle them through unique_ptr<T>
or shared_ptr<T>
handler classes.
Don't use pointers. Rarely use new
, and delete
never.
(Further tips: 1) Never use using namespace
unless it is for ADL. 2) Unless you are writing library components, well-designed classes should not have destructors, copy constructors or assignment operators. If they do, factor out the offending logic into a single-responsibility library component, then see 2).)