E
E
Egor Mikheev2016-07-12 09:38:51
C++ / C#
Egor Mikheev, 2016-07-12 09:38:51

How to understand how to use variables, pointers and references in C++?

Hello, I am learning C++
Theory regarding:

  • variables (copy)
  • pointer (address)
  • link (named address)

seems to understand.
But there were difficulties with understanding the practical application in OOP (implementation in classes)
In which case it is worth using references, pointers, references.
Of course, you can offer literature, but there seems to be no problem with literature, I would like to hear the understanding of those who constantly apply these methods in practice (OOP in C ++)
The question is more created as a tutorial, so it will be supplemented with examples over time.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Zhilin, 2016-07-12
@Anton3

In function parameter:
In return value:
In the class field:
Note 1. There are types that are expensive or impossible to move: large std::array, std::fstream. If we want to pass the owner of a variable of this type or, for example, return from a function, then we have to use std::unique_ptr.
Note 2. "Need to be modified" means that we want changes to the object to be visible outside the function. It is possible and necessary to get by with a constant reference, if it is permissible to copy a variable and modify it inside a function.

G
GavriKos, 2016-07-12
@GavriKos

Practice will save you. Over time, you will understand where you need it.
To get closer to understanding, you can try to answer the following questions yourself:
- what if you need to get two values ​​as a result of executing a function?
- you need to pass to the Bitmap function. How will you transmit it?
- you need to create a singly linked list. Describe its structure.
- you have two arrays of the same type. And there are two functions that modify an array of the same type in different ways. What will be the function prototypes?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question