Use of 'Const' in Function Return Values

为什么要在函数的返回值类型中添加Const?

1、Features

Of the possible combinations of pointers and ‘const’, the constant pointer to a variable is useful for storage that can be changed in value but not moved in memory.

Even more useful is a pointer (constant or otherwise) to a ‘const’ value. This is useful for returning constant strings and arrays from functions which, because they are implemented as pointers, the program could otherwise try to alter and crash. Instead of a difficult to track down crash, the attempt to alter unalterable values will be detected during compilation.

For example, if a function which returns a fixed ‘Some text’ string is written like

    char *Function1()
{ return “Some text”;}

then the program could crash if it accidentally tried to alter the value doing

    Function1()[1]=’a’;

whereas the compiler would have spotted the error if the original function had been written

    const char *Function1()
{ return "Some text";}

because the compiler would then know that the value was unalterable. (Of course, the compiler could theoretically have worked that out anyway but C is not that clever.)

2、Examples

Use of 'const' in Function Return Values

#include <iostream>
using namespace std;
const char * function()
{
return "some Text";
} const int function1()
{
return 1;
} int main()
{ char * h = function(); //1 does not compile
int h = function1(); //2 works return 0;
}

function returns a pointer to const char* and you can't just assign it to a pointer to non-const char (without const_cast, anyway). These are incompatible types.

function1 is different, it returns a constant int. The const keyword is rather useless here - you can't change the returned value anyway. The value is copied (copying doesn't modify the original value) and assigned to h.

For function to be analogue to function1, you need to write char* const function(), which will compile just fine. It returns a constant pointer (as opposed to a non-const pointer to a const type).

Use of ‘const’ in Functions Return Values的更多相关文章

  1. C++ 之const Member Functions

    Extraction from C++ primer 5th Edition 7.1.2 The purpose of the const that follows the parameter lis ...

  2. C++进阶--const和函数(const and functions)

    // const和函数一起使用的情况 class Dog { int age; string name; public: Dog() { age = 3; name = "dummy&quo ...

  3. [转]How to get return values and output values from a stored procedure with EF Core?

    本文转自:https://stackoverflow.com/questions/43935345/how-to-get-return-values-and-output-values-from-a- ...

  4. Tuples as return values

    Strictly speaking, a function can only return one value, but if the value is a tuple, the effect is ...

  5. HeadFIrst Ruby 第六章总结 block return values

    前言 这一章通过抽取一个文件中的确定的单词的项目进行讲解,主要包括了: File 的打开.阅读与关闭 find_all & refuse方法的相关内容 map 方法的相关内容这章的核心是:关于 ...

  6. 存储过程 返回值 procedure return values

    存储过程有三种返回: 1. 用return返回int型数据 2. 用返回参数返回结果,可以返回各种数据类型(通过游标来循环查询结果每一行) 3. 直接在存储过程中用select返回结果集,可以是任意的 ...

  7. [C++] CONST 2

    The C++ 'const' Declaration: Why & How The 'const' system is one of the really messy features of ...

  8. Use Reentrant Functions for Safer Signal Handling(译:使用可重入函数进行更安全的信号处理)

    Use Reentrant Functions for Safer Signal Handling 使用可重入函数进行更安全的信号处理 How and when to employ reentranc ...

  9. abiword Related Pages

    Application Framework The 'af' directory contains all source code for the cross-platform application ...

随机推荐

  1. Nginx的启动与停止,重启

    1.先确定nginx所在的文件位置 如: 重启 1.验证nginx配置文件是否正确 方法一:进入nginx安装目录sbin下,输入命令./nginx -t 2.重启Nginx服务 方法一:进入ngin ...

  2. 解决myeclipse4.1.1对一个表生成映射文件的时候,出现“generating artifacts"的解决!

    很多人在用myeclipse4.1.1对一个表生成映射文件的时候,都出现“generating artifacts"的问题.我也遇到了这个问题,弄得我也很郁闷!看了很多人的帖子后还是无法搞定 ...

  3. Ubuntu 安装 Oracle11gR2:'install' of makefile '/home/oracle/app/oracle/product/11.2.0/dbhome_1/ctx/lib/ins_ctx.mk'

    网上包括官方,就是教给你如何安装依赖包什么的:libstdc++5,但很麻烦:既要下载找相关的包,还不一定能安装的上. 其实,仅仅是为了安装,直接从二进制的deb包里,解压一个 “libstdc++. ...

  4. android SDK manager 快速更新【转】

  5. css文字超出自动显示省略号

    只针对单行文本有效: 01.针对块状元素 ul li{ width: 180px; text-overflow: ellipsis; white-space: nowrap;/*禁止自动换行*/ ov ...

  6. web开发中的安全问题

    web开发中很多东西由前段来负责判断,比如常见的邮箱 电话号码,前端判断到不是一个正确的格式,在你点击提交时候提示你格式填错了,然后不请求后端php,直到你填写正确的格式为止.这种其实可以修改js或者 ...

  7. 第四章 Spring.Net 如何管理您的类___IObjectPostProcessor接口

    官方取名叫 对象后处理器 (object post-processor) , 听起来很高级的样子啊!实际上就是所有实现了这个接口的类,增加了两个方法. Spring.Objects.Factory.C ...

  8. 工作流JBPM_day01:5-管理流程定义3点改进

    工作流JBPM_day01:5-管理流程定义3点改进 1.打包多个文件上传 再部署一个 查询所有看看,旧版本也查出来了 2.查询时只查询所有最新的版本 3.删除指定名称指定key的所有的它的版本

  9. Thinkphp 修改U方法按路由规则生成url

    tp开户路由后,使用U方法是不会按路由规则生成url的,一般我们是要手动修改模版,把里面的U方法去掉,手动修改链接,如果是已经写好的程序,后期才添加路由,修改起链接就太麻烦了 今天无聊就修改了一下U方 ...

  10. 【python】一次执行多个linux命令

    方法:多个命令之间用“;”进行连接即可: