c++中关于const的用法有很多,const既可以修饰变量,也可以函数,不同的环境下,是有不同的含义。今天来讲讲const加在函数前和函数后面的区别。比如:
06 |
// E-mail: wyphao.2007@163.com |
12 |
size_t length() const; |
13 |
const char* getPContent(); |
14 |
void setLengthValid(bool isLengthValid); |
17 |
size_t contentLength; //A |
18 |
bool lengthIsValid; //B |
19 |
size_t precontentLength; |
22 |
size_t TestClass::length() const{ //函数名后加const |
24 |
contentLength= strlen(pContent); //C |
25 |
lengthIsValid = true; //D |
31 |
const char* TestClass::getPContent(){//函数名前加const |
35 |
void TestClass::setLengthValid(bool isLengthValid){ |
36 |
lengthIsValid = isLengthValid; |
40 |
TestClass *tc =new TestClass; |
41 |
tc->setLengthValid(false); |
43 |
char * content = tc->getPContent(); //E |
其中类TestClass中的length函数和getPContent函数分别在函数名后和前加了const修饰符,如果试图编译上面的代码,将会得到下面的错误:
1 |
--------------------配置: mingw5 - CUI Debug, 编译器类型: MinGW-------------------- |
3 |
正在编译 C:\Users\wyp\Desktop\未命名1.cpp... |
4 |
[Error] C:\Users\wyp\Desktop\未命名1.cpp:24: error: assignment of data-member `TestClass::contentLength' in read-only structure |
5 |
[Error] C:\Users\wyp\Desktop\未命名1.cpp:25: error: assignment of data-member `TestClass::lengthIsValid' in read-only structure |
6 |
[Error] C:\Users\wyp\Desktop\未命名1.cpp:43: error: invalid conversion from `const char*' to `char*' |
7 |
[Warning] C:\Users\wyp\Desktop\未命名1.cpp:45:2: warning: no newline at end of file |
9 |
构建中止 未命名1: 3 个错误, 1 个警告 |
里面有三个错误,也就是代码C、D、E处的三个地方。为什么C和D处的代码会出错,原因如下:
length函数名的后面加了const修饰符,这样说明函数的成员对象是不允许修改的。我们都知道,在类的成员函数里面,默认是在成员函数的第一个位置
是this指针,如果在成员函数(只能是成员函数,要是类的静态函数或者是非成员函数就不可以在函数名后面加上const)后面const,则说明
this指针的值是不可以修改的,只能读取。而上面的length函数可能会修改里面的contentLength和lengthIsValid的值,这
样编译器肯定是不允许的,所以这样是会出现错误的。
解决方法是:在类的A、B处的成员前面加上mutable修饰符:
1 |
mutable size_t contentLength; //A |
2 |
mutable bool lengthIsValid; //B |
从字面的意思知道,mutalbe是“可变的,易变的”,跟constant(既C++中的const)是反义词。在C++中,mutable也是
为了突破const的限制而设置的。被mutable修饰的变量,将永远处于可变的状态,即使在一个const函数中。这样在C、D处将不会出错。
那么,为什么E处出现了错误。这是因为在函数名getPContent前加了const修饰符,意味着该函数返回的值只能是读取,而不能被修改。而E处的content却为char *是可以被修改的,这与const正好相反了,所以出现了错误。解决方法是:在char *前面加上const修饰符,即:
1 |
const char * content = tc->getPContent(); //E |
再去编译运行,这样就不会出现错误了。
from: http://www.iteblog.com/archives/214)
- C++函数前和函数后加const修饰符区别
class Test(){ public: Test(){} const int foo(int a); const int foo(int a) const; }; 一.概念 当const在函数名前 ...
- C++ 成员函数前和函数后加const修饰符区别
博客转载自: https://www.iteblog.com/archives/214.html 分析以下一段程序,阐述成员函数后缀const 和 成员函数前const 的作用 #include< ...
- 函数后面的const修饰符的作用
比如 void Fun() const; 的const是修饰什么的? 其实是修饰this指向的对象的. 这篇文章很详细的说明了const的作用,其中第三点说明了这种const的作用:const的用法, ...
- C++中的const成员函数(函数声明后加const,或称常量成员函数)用法详解
http://blog.csdn.net/gmstart/article/details/7046140 在C++的类定义里面,可以看到类似下面的定义: 01 class List { 02 priv ...
- 【VS开发】【C++开发】const在函数前与函数后的区别
const在函数前与函数后的区别 一 const基础 如果const关键字不涉及到指针,我们很好理解,下面是涉及到指针的情况: int b = ...
- C++: C++函数声明的时候后面加const
C++: C++函数声明的时候后面加const 转自:http://blog.csdn.net/zhangss415/article/details/7998123 非静态成员函数后面加const(加 ...
- const修饰符与函数
一.用const修饰函数的参数 函数参数类型前加const指明该参数为常量,在函数内部不可改变. void func(const int x) { //x不可以在内部进行赋值等操作. } 注:当参数为 ...
- Delphi 中 函数参数中的 const 修饰符的本质以及注意事项
来自:http://blog.csdn.net/farrellcn/article/details/9096787 ------------------------------------------ ...
- 字符串输出输入函数,const修饰符,内存分区,动态内存管理,指针和函数,结构体
1.字符串输出输入函数 读入字符串的方法: 1) scanf 特点:不能接收空格 2) gets 特点:可以接受含有空格的字符串 ,不安全 3) fgets(); 特点:可以帮我们自动根据数组的长度截 ...
随机推荐
- How Browsers Work: Behind the scenes of modern web browsers
http://www.html5rocks.com/en/tutorials/internals/howbrowserswork/#Parser_Lexer_combination Grammars ...
- ArcGIS Server发布服务,报错001270
错误001270 这个问题一般是因为数据源文件太大导致. 解决办法: 对于001270的错误,官方帮助中给出了一些可能的原因并提供了相应的解决办法(http://resources.arcgis.c ...
- Delphi下使用MapWinGIS控件打开GIS图层
unit Unit3; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- java的Random
首先,Point类 public class Point { int x, y; public Point(int x, int y) { this.x = x; this.y = y; } bool ...
- [LeetCode]题解(python):055-Jump Game
题目来源 https://leetcode.com/problems/jump-game/ Given an array of non-negative integers, you are initi ...
- 2014-4-25 运行号:837344 ASCII码排序
#include <iostream> #include <cstdio> #include <cstdlib> #include <string> # ...
- Mac版 MicrosoftOffice2015 办公软件 破解教程
来自:http://bbs.feng.com/read-htm-tid-9704285.html 下载链接:http://pan.baidu.com/s/1dD6lBFz 提取密码:xu5n 补丁: ...
- 如何查看Servlet、JSP的版本(Tomcat V7.0.70)
1. 简要说明:Tomcat6.0 所支持的是Servlet2.5,Tomcat 7.0 所支持的Servlet3.0,Servlet2.5 和Servlet3.0的差异较大,对于Servlet3.0 ...
- 系统UITabBar属性设置
设置背景: [_tabBar setBackgroundImage:[UIImage imageNamed:@"bg_tabbar"]]; 设置某个Item选中的效果: _tabB ...
- JQuery:通过noConflict()方法同时使用jQuery 和其他框架
jQuery - noConflict()方法 一.如何在页面上同时使用 jQuery 和其他框架?jQuery 和其他 JavaScript 框架正如您已经了解到的,jQuery 使用 $ 符号作为 ...