c++ - Const keyword in function with an *& argument. -


can please explain why in following code:

    #include <iostream>     void fun(char * const & x){(*x)++;}     int main(){          char txt[100]="kolokwium";           fun(txt);         std::cout << txt <<"\n";     } 

keyword const needed code compile?

if remove get:

 invalid initialization of non-const reference of type ‘char*&’ rvalue of type ‘char*’ 

thanks!

txt of type char[100]. has converted char * in order passed fun; conversion produces rvalue. cannot create non-const reference rvalue.

to illustrate, consider happen if fun defined follows:

void fun(char *&x) { x++; } 

what following code (assuming compile)?

char txt[100]="kolokwium"; fun(txt);                      // huh? 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -