C 中的常量返回类型
我正在阅读一些代码示例,它们返回了 const int。当我尝试编译示例代码时,出现了有关返回类型冲突的错误。所以我开始搜索,认为 const 是问题所在(当我删除它时,代码工作正常,不仅可以编译,而且按预期工作)。但我从未能够找到专门与 const 返回类型相关的信息(我为结构/参数/等找到了信息,但没有找到返回类型)。所以我尝试编写一段代码来简单地展示 const 可以做什么。我想出了这个:
#include <stdio.h>
int main() {
printf("%i", method());
}
const int method() {
return 5;
}
当我编译这个时,我得到:
$ gcc first.c
first.c:7: error: conflicting types for ‘method’
first.c:4: note: previous implicit declaration of ‘method’ was here
但是,每当我删除 const 时,它都会如预期的那样简单地打印出 5,a 继续生命。那么,谁能告诉我 const 用作返回类型时应该意味着什么。谢谢。
I was reading some samples of code, and they returned a const int. When I tried to compile the examples code I got errors concerning conflicting return types. So I started searching, thinking that the const was the problem (when I removed it, the code worked fine, not only did it compile, but worked as expected). But I never was able to find information specifically pertaining to a const return type (I did for structures/parameters/etc. etc., but not return types). So I tried writing a piece of code to simply show what const may do. I came up with this:
#include <stdio.h>
int main() {
printf("%i", method());
}
const int method() {
return 5;
}
And when I compile this, I get:
$ gcc first.c
first.c:7: error: conflicting types for ‘method’
first.c:4: note: previous implicit declaration of ‘method’ was here
However, whenever I remove the const, it, as expected, simply prints out a 5, a continues on with life. So, can anyone tell me what const is supposed to mean when used as a return type. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
const
对于返回值没有任何意义,因为返回值在任何情况下都是右值并且无法修改。您收到的错误是由于您在声明函数之前使用了该函数,因此隐式假定返回int
,而不是const int
但当该方法实际上已定义,返回类型与原始假设不匹配。如果返回double
而不是int
,您会得到完全相同的错误。例如:
生成:
看看调高警告级别有多大帮助!
const
makes no sense for return values because return values are rvalues in any case and can't be modified. The error you are getting is from the fact that you use a function before it has been declared so it is implicitly assumed to returnint
, notconst int
but then when the method is actually defined, the return type doesn't match the original asssumption. You would get exactly the same error if it were, say, to returndouble
instead ofint
.E.g.:
generates:
See how helpful it is to turn the warning levels up!
在调用 method() 之前添加 method() 的原型将修复该错误。
此错误告诉我们
method()
是由编译器创建的(因为它没有找到它),其返回类型与const int
(可能是 int)不同。这个另一个错误告诉我们,实际上编译器创建了自己的方法版本。
Adding the prototype of method() before you call it will fix the error.
This error tells us that
method()
was created by the compiler (because it didn't find it) with a different return type thanconst int
(probably int).This other error tells us that in fact the compiler created its own version of
method
.main
发现使用了没有原型的method()
,因此它假设它返回 int。然后将其声明为返回const int
。将method()
的声明移到main
之前,或者将原型放在main
之前。main
sees a use ofmethod()
without a prototype, so it assumes it returns int. Then you declare it as returningconst int
. Move the declaration ofmethod()
beforemain
, or put a prototype beforemain
.您发布的代码至少应该为您提供一个未定义的标识符:
method
。在调用该函数之前,您需要在范围内进行声明。更好的使用:定义也是声明。所以,这应该可以解决您的错误。
The code you posted should give you an undefined identifier:
method
at the very least. You need a declaration in scope before you can call the function. Better use:A definition is also a declaration. So, this should fix your error.
当您使用某个函数时,在您向 C 详细介绍该函数之前,C 会猜测该函数的返回类型——它的名称、返回类型、常量性和参数。如果这些猜测是错误的,你就会得到错误。在这种情况下,他们错了。使用原型或将函数移至调用上方。
哦,关于 CONST 性:
这意味着如果您使用相同的参数再次调用函数,该函数的值将是相同的,并且应该不会有(重要的)副作用。这对于优化很有用,而且它还以书面形式声明编译器可以强制执行相关参数。函数承诺不改变常量,编译器可以帮助阻止它。
C makes guesses about the return type of a function when you use it before you've told C enough about the function -- it's name, return type, const-ness, and arguments. If those guesses are wrong, you get the error. In this case, they ARE wrong. Use a prototype or move the function above the call.
Oh, and about CONST-ness:
this means that the value of a function will be the same if you call it again with the same parameters, and that there should be no (important) side effects. This is useful for optimization, and also it makes a documentary claim that the compiler can enforce concerning parameters. A function promises not to alter a constant, and the compiler can help prevent it.