Let's see if someone can indicate to me, that I am misunderstanding, when it comes to understanding the concept const
.
I have an attribute of my class defined as follows:
std::string name=""; //Inicialiazado Author::Author(const std::string & nam,const std::string * email, char gender){
name=nam;
Author::setEmail_2(email);
if(Author::checkGender(gender)){
this->gender=gender;
}else{
this->gender='u';
}
}
.
My constructor looks like this:
std::string name=""; //Inicialiazado Author::Author(const std::string & nam,const std::string * email, char gender){
name=nam;
Author::setEmail_2(email);
if(Author::checkGender(gender)){
this->gender=gender;
}else{
this->gender='u';
}
}
.
I do not explain all the functions of its interior, since my doubt is centered on the attribute name
. As the constructor is defined, I have to pass a reference to an object of type string
, and that this object can not be manipulated when marked with const
.
I do not understand why it compiles to me, if in principle nam
(builder's parameter) is of type const str::string
, I would not have to define my attribute as const
as well (since when I set name=nam
, I am happening to name
the address of nam
) because if I do not define const
, I could manipulate the object passed by parameter, which input did not want, so I put the parameter was const
. I do not know why it works for me I would have put the const
attribute.