Home > Back-end >  The meaning of c programming fundamentals, the relevant &
The meaning of c programming fundamentals, the relevant &

Time:09-20

Today in learning c + + inheritance of relevant knowledge, and then in the book, I saw a piece of code, without understanding, the search on the net also not understand, ask colleagues, since the people know what do you mean?
Procedure is as follows: is the thumb on the right side of the other code, the string followed "& amp;" , like and reference not

CodePudding user response:

Returns the reference,

CodePudding user response:

If so you can access the private members of the base class, but how specific operation, he and the preceding function elegantly-named setName () is supporting you, that is, the member function in a subclass, as long as the call elegantly-named setName () function, already can access private members in the base class, namely string& GetName () function returns the value

CodePudding user response:

I think here is not necessary to use references, with reference to have side effects (the reference try) :

//Note: Your choice is a c + + IDE 
#include
#include
using namespace std;

The class Person
{
Private:
string name;
Public:
The Person (string name)
{
This - & gt; Name=name;
}
String& GetName ()
{
return name;
}
};

Int main ()
{
The Person p1 (" Tom ");
CoutP1. The getName ()="Mike";
Coutreturn 0;
}


By getName () to modify the name, this should not expect,
You what is the name of the book, the author?
  • Related