c – int 1的默认值是?
发布时间:2020-12-16 10:51:05 所属栏目:百科 来源:网络整理
导读:从 this question那里学到了在C It is okay to put the name of the variable in parenthesis 我试过这个程序: #include iostreamint main(){ int (a)(); std::cout "if this works then deafult value of int should be " a std::endl; return 0;} 得到了
从
this question那里学到了在C
It is okay to put the name of the variable in parenthesis
我试过这个程序: #include <iostream> int main() { int (a)(); std::cout << "if this works then deafult value of int should be " << a << std::endl; return 0; } 得到了‘if this works then deafult value of int should be 1’的输出 编辑:: 所以现在很清楚,这里的a是函数而不是变量. 解决方法
a不是int:它是一个没有参数并返回int的函数.因为它是一个函数声明,所以a也不是局部变量,并且它没有“默认值”.
该程序格式错误,因为您从未定义函数a但您尝试使用它(通过在插入表达式中获取其地址).因此它违反了一个定义规则. 如果在程序中定义a,则将打印1,因为函数a的地址将转换为bool:运算符<<具有bool参数的重载是函数指针参数类型的最佳匹配. [注意:如果使用Visual C定义并编译,它将打印函数的地址,而不是1.这是(我认为)因为Visual C允许函数指针隐式转换为void *,然后运算符< ;<具有void const *参数的重载是函数指针参数类型的最佳匹配.如果在禁用语言扩展名(/ Za)的情况下进行编译,则将按预期选择带有bool参数的重载. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |