What is the correct way to create objects in C ++?

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

method 1

QString str = "my string"; 

method 2

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

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

  • What is the difference between the two?
  • Which method should I stick with?
  • when to use method 1 correctly and when to use method 2 correctly?
  • in method 2, I can destroy the object by calling delete str; . How can I remove the str variable when using method 1?

thanks

+6
source share
6 answers
  • First of all, they have different lifetimes: the object created in method 2 will live as long as you like until you name delete; in method 1, it will be created on the stack and will be destroyed when it returns from the function call (if any). second method 2 requires more work due to non-trivial memory management.

  • Use a method that matches your desired lifetime. if the lifetime of method 1 is good enough so that you do not use method 2, since it entails the overhead of memory management. if you can restructure your program so that you can do it using method 1 and also improve the design, it will be more efficient and elegant.

  • see above. in particular, using method 1 and storing a pointer to an object and accessing it after its life cycle is a trap. it is also possible with the help of method 2, but explicit destruction focuses the programmer's attention on it (but, nevertheless, since the lifetime is not direct, this is a possible mistake). The trap with method 2 forgets to delete it, causing a memory leak (or deleting it too early and referring to it as described above in this paragraph)

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

+5
source

These two things are very different.

QString str("my string"); creates an object whose lifetime is automatically controlled: the object either lives until the end of its covering area (if it is a local variable), or until the end of the program (if it is a global variable).

new QString("my string"); creates an object with a manually controlled lifetime, also called a "dynamic object" (and returns a pointer to this object). This means that you are responsible for managing the lifetime of the object. It is almost never worth doing if you are not writing a library component.

And here lies the essence of the C ++ philosophy: C ++ is a language for writing libraries. You have tools for writing high-quality, reusable components. If and when you do this, you will need to know the intricacies of lifecycle management. However, until the time comes, you must use existing library components. By doing so, you will find that you will hardly need to perform manual control at all.

Use dynamic containers (vectors, lines, maps, etc.) to store data and create your own data structures. Pass arguments by reference if you need to modify objects in the call area. Create complex classes from simpler components. If you really need to have dynamic objects, process them through the handler classes unique_ptr<T> or shared_ptr<T> .

Do not use pointers. Rarely use new and delete never.

(More tips: 1) Never use using namespace unless it is for ADL. 2) If you are not writing library components, well-designed classes should not have destructors, copy constructors, or assignment operators. If they do, drop the insult logic into the library component with one responsibility, see 2).)

+5
source

When you use the first syntax, you create an object named str with type QString and the initial value is "my string" .

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

Pointers do not hold a value, they point to a memory location where the value is stored, and the difference is large.

+3
source

Method one will use automatic memory management (the object will be deleted when it goes beyond the bounds).

The second method is intended for manual memory management - it will not be deleted until delete str; called delete str; If you forget to delete it, this creates what we call a memory leak!

Normally, a method would be a better choice if you have no reason to use a pointer. (less chance of mistakes)

You may find this useful: Why use pointers?

+2
source

The difference is that the method creates a QString on the stack, and the second method creates a QString in free storage. (Note that QString s = "hello"; exactly matches QString s("hello"); ;.) Like parapura rajkumar, always do 1 when you can, and 2 when you cannot do 1.

Method 1 has many advantages, not the least of which is automatic memory management. The memory occupied by this QString will be automatically released when it goes out of scope, so you do not need to do anything to free up memory. Method 2 requires you to use delete when you are done with this to free up memory, or you will have a memory leak.

Another advantage is that it’s much faster to create something on the stack than in free storage.

The situation in which you should use method 2 is when you need this object to last longer than the area in which you are located. Then you would put in the free storage with new so that it lasts until you call delete , and then pass the pointer.

+2
source

when to use method 1 correctly and when to use method 2 correctly?

In this example, it is not obvious when you can use a pointer. But if you create your own class, such as Images, which contains a lot of data, and you want to pass an object of this class in a function or method, I suggest you use a pointer to the object, not the object itself. What for? If you pass a pointer to a function, you only pass the memory address of the object (you copy a few bytes), but if you pass the object itself, then you transfer a lot of data, which can slow down your application.

Let's look at another example: Suppose you have a function with three parameters, and the body of the function must change each parameter when the function ends. As you know, a function can return only one value, so one way to change each parameter in the body of a function is to use pointers.

There are many reasons for using pointers, and you should be careful when using them. I suggest you read a few articles about this topic.

I hope you understand the difference. The key concept is the difference between the address of the object and the object itself!

+1
source

Source: https://habr.com/ru/post/903422/


All Articles