关于常量形参,这个代码为什么编译器不报错

#include<iostream>

using namespace std;


int square(const int& a)
{
    return a*a;
}
int main()
{
    int b=4;
    b=square(b);
    cout<<b;
    return 0;

}

 

为什么应该报错?题主修改代码后,squre 函数内并没有修改函数参数a,你试试在squre函数内修改a再编译下。

#include<iostream>

using namespace std;


int square(const int& a)
{
    return a*a;
}
int main()
{
    int b=4;
    b=square(b);
    cout<<b;
    return 0;

}