小编as *_* df的帖子

C++)为什么 const int*& 参数不能采用 int* 参数?

在写作之前,我的英语不好。所以可能会有很多尴尬的句子。

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

c++ reference constants

2
推荐指数
1
解决办法
232
查看次数

标签 统计

c++ ×1

constants ×1

reference ×1