An explanation, why are the speakers?
No, I understood the principle of the work, but I didn’t understand one thing - WHAT? Why work with the member functions alternating with the reference if I can work with them indirectly?
An explanation, why are the speakers?
No, I understood the principle of the work, but I didn’t understand one thing - WHAT? Why work with the member functions alternating with the reference if I can work with them indirectly?
"If someone lights the stars, then it needs someone"
Here is an example. Suppose you pass a variable to the function. There, with its value, something is being done, and the new value should be entered into the variable whose value you passed to the function. If you do something like this
void func (int a) { a += 10; } ...... int i = 2; func(i);
That value of the variable i will not change, because a copy of this variable is passed to the function and operations are performed with it. And if you pass a pointer or a link to this variable, you will get access to it and you can change its value.
void func (int* a) { *a += 10; } ...... int i = 2; func(&i);
If you need to transfer a structure or object to a function, you can transfer it entirely, but it can take a lot of memory and time, because they can be very large. In simple examples, this is imperceptible, but in real programs, where it can be repeated thousands and millions of times, the brakes will be huge. Instead, pass a pointer or a link and get access to the object from the function.
Already talked about dynamic memory allocation. If you have not read about it yet, then read it. There without pointers IN PRINCIPLE not enough.
These are just a few examples. The deeper you go into C ++, the more you will learn about it.
And further. Do not think that programmers before you were fools.
Pointers apply:
To return multiple values from a function. As an argument, a pointer to the variable is passed, the function writes the value there. This approach is very common in DirectX, OpenGL, Windows API and other C-style libraries. You can also use links for this, but it is not recommended, since the syntax for sending and returning is indistinguishable.
To store the address of the dynamically allocated memory. It differs from the usual one in that the programmer himself regulates the lifetime of objects, and it is longer (and the stack size is only about a megabyte). If the address is lost, the memory can neither be used nor freed. A memory leak will occur.
C-string is a pointer to its first character.
To create various data structures: linked lists, trees, etc.
To pass an argument to a function without copying (and calling the constructor for objects), which can be long for complex objects. True, it is better to use constant links.
Thus, there are a lot of pointer applications.
To allocate a large amount of memory. I advise you to read about the stack and heap. A large amount of data on the stack will overflow.
Source: https://ru.stackoverflow.com/questions/67664/
All Articles