Operation of const type variables

4

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.

    
asked by Jcpardo 25.04.2018 в 21:23
source

3 answers

2
  

I do not understand why it compiles to me, if in principle nam (constructor parameter) is of the const str :: string type, I would not have to define my attribute as const as well (since when assigning name = nam, I'm passing it to name the address of nam)

You're wrong.

name is a member of type std::string , then when you call the assignment operator a copy of the object referenced by nam is made.

Look at what would be the typical declaration of the assignment operator:

std::string& operator=(std::string const&);

That is, you receive a constant reference , which is exactly what you are going through ... and will internally copy the state of the object pointed to by that reference. After making the assignment, the state of name will be independent of the state of nam and the changes you make in one of these variables will not affect the other ... they are two independent objects.

Another thing is that name was a reference (as is the case with your other question):

std::string & name;

In this case you would not be invoking the assignment operator of class std::string but the program will try to make name refer to the same object as nam ... and here the problems will start because nam is a constant reference and name no:

std::string cadena;

std::string& cad1 = cadena;       // OK, cad1 referencia a cadena
std::string const& cad2 = cadena; // OK, cad2 referencia a cadena (pero no puede modificarla)
std::string& cad3 = cad1;         // OK, cad3 referencia a cadena
std::string const& cad4 = cad1;   // OK
std::string & cad5 = cad2;        // ERROR: cad2 es referencia constante
std::string const& cad6 = cad2;   // OK, cad6 referencia a cadena

cad1 = "hola";
std::cout << cadena << '\n';      // imprime hola
std::cout << cad2 << '\n';        // imprime hola
cad2 = "adios";                   // ERROR: cad2 es una referencia constante
    
answered by 26.04.2018 в 07:20
1
  

I do not understand why it compiles to me, if in principle nam (builder's parameter) is of type const str::string , would not I have to define my attribute as const as well?

I'll tell you a secret but do not tell anyone !: " Sometimes I see dead people ".

Now you know my secret, now we are two people who know my secret, you can not change my secret (although you say that sometimes I see tardigrades, it will not change the fact that I see dead) now that you know you could do with that information what you want.

Moving this story to C ++, my secret is const (you can not change something I've lived) but even if you can not change my experience, you can write it in a notebook (the notebook is not const ) , and that is precisely what is happening:

void f(const std::string &secreto) {
    // Copiamos el secreto
    std::string libreta = secreto;
    // Lo modificamos
    libreta = "No te creo, mientes";
    // Pero como no te creo, ¡cambiaré tu secreto!
    secreto = "En ocasiones veo tardígrados"; // <-- Error de compilación, 'secreto' es const
}

int main() {
    const std::string texto = "En ocasiones veo muertos";
    f(texto);
    return 0;
}

In other words, constant objects are read-only; the read-only characteristic is not contagious around it, it only tells us that the constant object can not be modified.

    
answered by 26.04.2018 в 07:55
0

When you assign nam to mame, the string class has the assignment operator overloaded and in that case it makes a copy of nam in name, so if you modify name it will not change nam.

link

    
answered by 26.04.2018 в 02:16