Answer the question
In order to leave comments, you need to log in
What is the difference between an object reference and an object data member?
I will give an example of the detail of interest to me on the copy constructor
constructor_and_d_and_c::constructor_and_d_and_c(const constructor_and_d_and_c& c) // передаем объект, который хотим скопировать
{
cout << "To, chto interesuet: " << c.n_Name << endl;
cout << "copy is calling" << endl;
n_Name = new string(*(c.n_Name)); // n_Mame - член данных нового объекта
};
Answer the question
In order to leave comments, you need to log in
The symbol & in C++ has two different meanings depending on where it is used:
1) When & is used in a type declaration, it denotes that the type is a reference type.
Example:
int b = 5;
int &a = b; //a - это объект-ссылка тип int. Инициализирован объектом b
int b = 5;
int * a = &b; //а - это указатель на объект типа int. Инициализирован адресом объекта b
A link is not an address, an address is a pointer.
n_Name is declared apparently as String *n_Name - i.e. n_Name is a pointer, so by calling c.n_Name you get the address of the object.
Think of references as aliases (aliases) of existing variables. References should be treated like regular variables, not like pointers.
Assumptions:
The function definition specifies how the argument is passed. It is indicated that it is passed by reference, but this is a technical detail, but formally, inside the c function, this is not an address, but the object itself.
If you need to send an address, then the description should be *
An object can have multiple data fields. The address of the first field and the object are usually the same.
& in the function body means taking the address. The code
will give the address of the object.
The code
will give the address of the field, and it will probably match the address c.
Judging by what's going on. n_Name is the address of the memory that is allocated to hold the string. It is not entirely clear why this was done, it was possible to make a string directly a class field, and not an address to it. But one way or another, a variable containing an address also has an address.
Therefore, it
gives out some address. But it is not the address of the c object or the c.n_Name field. This is the address of the dynamically allocated memory for the string.
At the address of the string, using *, we get a string object, then we create a new string, we write the address in n_Name.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question