譬如int GetData()const;
这个const起什么作用,能具体说明一下吗,谢谢.
C++中函数的返回值为const类型的作用
答案:2 悬赏:50 手机版
解决时间 2021-02-28 06:00
- 提问者网友:你挡着我发光了
- 2021-02-27 09:28
最佳答案
- 五星知识达人网友:不甚了了
- 2021-02-27 10:00
If you are returning an object of a user-defined type by value as a const, it means the returned value cannot be modified. If you are passing and returning addresses, const is a promise that the destination of the address will not be changed.
全部回答
- 1楼网友:患得患失的劫
- 2021-02-27 10:50
形参是char *类型,实参是cons chart *类型时,编译程序会给出错误信息,意思是被调函数可以改变串的内容,而实参又是const类型,这样的实参不允许改变,二者之间不能融合,即使函数不改变形参的内容也不行。
但形参是const char *,实参是 char *是可行的,当要返回形参指针时,其返回类型必须是const char *。
#include
#include
const char *strfind(const char *str,const char *substr) {
int i,j;
int m = strlen(str);
int n = strlen(substr);
for(i = 0; i <= m - n;++i) {
for(j = 0;j < n;++j) {
if(str[i + j] != substr[j]) break;
}
if(j == n) {
return str + i;
}
}
return null;
}
int main() {
char s[] = "please sit down.";
char t[] = "down";
const char *pstr = strfind(s,t);
if(pstr) printf("yes.\n");
else printf("no.\n");
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯