Use of C++ References

Asked 2 years ago, Updated 2 years ago, 24 views

Question 1)

Why is the return type of a function used as a reference type? I can't really relate to it. Does it work well with more than just a reference to name a variable?

I can't relate to all the examples in the book because they seem to be full of meaningless codes to forcefully explain the reference type. I wonder how to understand it to be neat and when it is mainly used.

Does it exist simply for Call-By-Reference?

Question 2)

In the case of the image above, the reason why the parameter of the Adder is received as a reference type,

I don't know why I declare (Point *pptr, Point *pptr2) by writing the pointer to the variable to be passed as a parameter of the function.

Is it designed to study the keyword new meaninglessly just because it is an example?

Is it right that you don't have to squeeze it like the picture when you actually squeeze it?

Example: Point Adders (Point p1, Point p2); I think it's enough.

Question 3)

The pptr inside the Adder function in the picture is dynamically assigned to the new keyword as a regional variable The local variable disappears when it is off the scope, does it automatically delete it, i.e. memory is released?

And shouldn't regional variables be returned as references? It's not working, but it's a pointer, so I can't release it Are you forcing yourself to do it because you're staying?

It is very difficult to analyze what the above example means. I have no idea what it is to gain.

Question 4)

Write a reference as a parameter for a function, but if you do not change the value inside the function

Use the const keyword with void func(constant &num); It's a rule to mark it, and I'm not going to change the value inside the function Why do you declare a parameter as a reference? I'm leaving a question because I think it doesn't add up.

c++

2022-09-22 15:46

2 Answers

Looking at the questioner's question...First of all, it seems necessary to learn the memory structure that the program uses to perform. In fact, c/c++ has many characteristics of low-level languages (Assemblies), so there is a lot to study compared to advanced languages (JAVA, PYTHON, GO, etc.) with GC.

When the program runs as simple as possible, use memory in two different forms. The first is the stack and the second is the heap.

As I mentioned in the previous question, regional variables are stored in a stack, and specifically, a stack is a structure in which a dedicated stack frame is created for each function and the stack frame is organized when it exits the function (returning).

By the way, if you create it with the new keyword (where c is a memory allocation function, such as malloc), you allocate memory to a space called a heap. The memory space called heap is just linear long (large) memory space. Unlike the stack, the memory allocated here is not automatically cleaned up, so you must explicitly delete or free it.

If you create an object with new in the region variable pointer within the function and return the region variable pointer, there is no problem because it returns a valid heap address. However, you must release it using the returned pointer address so that the memory does not leak.

An example of a book is an example that you created to explain what you learned. In fact, in the field, the new keyword is refrained, the smart pointer is used, and the data structure is also used to implicitly induce memory release as much as possible.


2022-09-22 15:46


2022-09-22 15:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.