Which is the right way to instantiate objects in C++?

In C++ (I use QT) I can create an instance of QString class two ways:

method 1

QString str = "my string";

method 2

QString *str = new QString("my string");

I know this is to do with pointers. So my questions are:

  • what is the difference between the two?
  • which method should I stick to?
  • when is it correct to use method 1 and when is it correct to use method 2?
  • in method 2 I can destroy the object by calling delete str; . How can I delete the str variable when using method 1?
  • Thanks


  • primarily they have different lifetimes: the object created in method 2 will live arbitrarily long until you call delete; in method 1 it will be created on the stack and be destroyed upon return from the function call (if any). secondarily method 2 requires more work because of non-trivial memory management.

  • use the method that matches the lifetime you want. if the lifetime of method 1 is good enough for you do not use method 2 because it entails the overhead of memory management. if you can restructure your program so that you can do with method 1 while also improving on the design, that will be more efficient and elegant.

  • see 2. above. in particular, using method 1 and storing a pointer to the object and accessing it after its lifetime is a pitfall. this is also possible with method 2 but the explicit destruction focuses the programmer's attention on it (but still because the lifetime is not straightforward it is a likely pitfall) a pitfall with method 2 is forgetting to delete it causing a memory leak (or deleting it too early and referring to it as described earlier in this paragraph)

  • in method 1 the object will be automatically deleted when the function returns.


  • 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).)


    When you use first syntax, you are creating object whose name is str with a type of QString and an initial value of "my string" .

    In second declaration you create pointer of type QString whose name is str .

    Pointers do not hold the value, they point on the memory location where the value is stored, and the difference is big.

    链接地址: http://www.djcxy.com/p/13806.html

    上一篇: c ++:新Object()和Object()之间的区别是什么

    下一篇: 哪种方法可以在C ++中实例化对象?