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. page指令属性简要介绍:

    page指令属性简要介绍: language=”java” 声明脚本语言的种类,暂时只能用”java” extends=”package.class” 标明JSP编译时需要加入的Java Class的 ...

  2. Spring-注入外部值

    Spring注入需要初始化,但前面均使用硬编码注入,如: JavaConfig配置: package soundSystem; import org.springframework.stereotyp ...

  3. 关于java之socket输入流输出流可否放在不同的线程里进行处理

    2014年2月20日到叫(黑土)(人士)的公司去面试,一家新成立的公司.刚去公司是他们新聘请的猎头A来面试我的,A面试完之后是一个号称X总的年轻人来面试我,初一见此人有点邋遢,穿着西装. X:&quo ...

  4. 编译ros程序包--4

    编译程序包(原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/) 1.编译程序包: 一旦安装了所需的系统依赖项,我们就可以开始编译刚才创建的程序包了 ...

  5. Java------遍历Map<k,v>的方法

    1. public class MapAction extends ActionSupport{ private Map<String, User> map = new HashMap&l ...

  6. laravel 发送邮件

    1)邮件配置(config/mail.php 配置文件) MAIL_DRIVER                       邮箱驱动,laravel 支持 "smtp", &qu ...

  7. break、continue、return之间的区别与联系

    今天在部署程序的时候,监控日志发现这个问题了.return的问题就这么总结哈. 在软件开发过程中,逻辑清晰是非常之重要的. 代码的规范也是非常重要的.往往细节决定成败.在编写代码的时候,一定要理解语言 ...

  8. eclipse中debug模式不能启动运行,run运行模式却能启动运行!

    这个问题我郁闷了好久!问题原因:因为断点太多了,断点冲突了. 解决办法:只要进如deug界面,选择BreakPoints选项,然后清除所有断点,再重新debug启动.问题解决! 希望能帮到遇到此问题的 ...

  9. canvas二:绘制圆和其他曲线

    1.绘制圆 绘制圆是canvas里面不可缺少的功课,而且绘制圆在canvas中的用处很多,好嘞,开扯 绘制圆需要用到arc这个方法: arc(X坐标,Y坐标,半径,起始弧度,结束弧度,旋转方向): 弧 ...

  10. Error setting expression 'XXX' with value 设置表达式“XXX”时出错 解决方法

    1.表达式“xxx”在所调用的action里没有与之对应的对象: 2.action里有该对象作为私有成员变量但是没有get&set方法.