Use of ‘const’ in Functions Return Values
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的更多相关文章
- C++ 之const Member Functions
Extraction from C++ primer 5th Edition 7.1.2 The purpose of the const that follows the parameter lis ...
- C++进阶--const和函数(const and functions)
// const和函数一起使用的情况 class Dog { int age; string name; public: Dog() { age = 3; name = "dummy&quo ...
- [转]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- ...
- Tuples as return values
Strictly speaking, a function can only return one value, but if the value is a tuple, the effect is ...
- HeadFIrst Ruby 第六章总结 block return values
前言 这一章通过抽取一个文件中的确定的单词的项目进行讲解,主要包括了: File 的打开.阅读与关闭 find_all & refuse方法的相关内容 map 方法的相关内容这章的核心是:关于 ...
- 存储过程 返回值 procedure return values
存储过程有三种返回: 1. 用return返回int型数据 2. 用返回参数返回结果,可以返回各种数据类型(通过游标来循环查询结果每一行) 3. 直接在存储过程中用select返回结果集,可以是任意的 ...
- [C++] CONST 2
The C++ 'const' Declaration: Why & How The 'const' system is one of the really messy features of ...
- Use Reentrant Functions for Safer Signal Handling(译:使用可重入函数进行更安全的信号处理)
Use Reentrant Functions for Safer Signal Handling 使用可重入函数进行更安全的信号处理 How and when to employ reentranc ...
- abiword Related Pages
Application Framework The 'af' directory contains all source code for the cross-platform application ...
随机推荐
- Java运行时,各种类型存储介绍
Java的内存分配 Java程序运行时的内存结构分成:方法区.栈内存.堆内存.本地方法栈几种. 方法区 存放装载的类数据信息,包括:基本信息:每个类的全限定名.每个类的直接超类的全限定 ...
- 深入解析AsyncTask
REFRENCES:http://blog.csdn.net/hitlion2008/article/details/7983449 AsyncTask的介绍及基本使用方法 关于AsyncTask的介 ...
- c++ word类型
word就是16位的数据 随着机器的发展,C++语言本身并没有规定short的位数,不一定是十六位的(随着计算机的发展,可能改变).但word将永远是16位的--机器发展后只需要修改,typedef ...
- Case用法
SELECT <myColumnSpec> = CASE WHEN <A> THEN <somethingA> WHEN <B> THEN <so ...
- linux环境下,清空history中记录的历史命令
需求描述: 今天在数据库主机上操作,通过history看到有刚操作过的历史记录,想把这个清除了, 但是,还不影响后续的命令记录,所以查了下,在此记录. 操作过程: 1.通过history -c命令,完 ...
- JBOSS-EAP-6.2集群部署
1 概述 应用的合理部署即能提高系统的可靠性和稳定性,又能提高系统的可维护性和扩展性.本文档详细阐述基于Apache负载均衡和JBOSS7集群的应用系统部署方案和配置步骤.内容涉及部署方案.环境配置. ...
- 从头认识java-16.5 nio的数据转换
这一章节我们来讨论一些nio的数据转换. 上面一章节我们提到ByteBuffer,可是他是面向二进制数据,对于编程来说不是非常方便,因此,java添加了转换数据的工具. 1.asCharBuffer ...
- Java精选笔记_Servlet技术
Servlet技术 Servlet开发入门 Servlet接口 针对Servlet技术的开发,SUN公司提供了一系列接口和类,其中最重要的是javax.servlet.Servlet接口. Servl ...
- Ehcache整合spring
下面介绍一下简单使用的配置过程:ehcache.jar及spring相关jar就不说了,加到项目中就是了. 简单的使用真的很简单.但只能做为入门级了. 1.ehcache.xml,可放classpat ...
- raw_input()
raw_input() 用于接收标准输入,并把标准输入当成字符串类型来处理,只能在 Python2 中使用,Python3 中没有这个函数 #!/usr/bin/env python #-*- cod ...