mardi 4 septembre 2018

Difference between const and non-const reference

What is the difference between const and non-const reference?

const reference variable:

#include <iostream>

int main() 
{
    const int &i = 10;// compiles
}

Compile fine.

But, non-const reference variable:

#include <iostream>

int main() 
{
    int &i = 10; // Not compiles
}

Error:

prog.cc: In function 'int main()':
prog.cc:5:14: error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'
     int &i = 10; // compiles

Aucun commentaire:

Enregistrer un commentaire