在写作之前,我的英语不好。所以可能会有很多尴尬的句子。
void Func1(const int* _i) { };
void Func2(const int& _j) { };
void Func3(const int* (&_k)) { };
int main()
{
int iNum = 1;
int* pInt = new int(1);
Func1(pInt); // works;
Func2(iNum); //works
Func3(pInt); // error
}
Run Code Online (Sandbox Code Playgroud)
我使用 Visual Studio,错误消息显示“无法将参数 1 从 'int *' 转换为 'const int *&'”
我知道它无法转换,因为“&”。_i 等于 pInt,因此它可能会更改取消引用。但我用的是const。所以我认为它会起作用,但 const 关键字不起作用。为什么 const 关键字与其他情况不同?例)Func1,Func2