打破C++ Const 的规则
从一个C++菜鸟改函数开始
CString MyClass::GetStringValue() const
{
return m_strValue;
}
这个值可能还没有赋值,好吧,那么我先判断是不是为空,为空就赋值了
CString MyClass::GetStringValue() const
{
if(m_strValue.IsEmpty())
SetStringValue(); return m_strValue;
}
结果,编译就不过,因为有个规则:const函数里面不能调用非const函数。
看到下面的编译错误:
error C2662: “MyClass::SetStringValue”: 不能将“this”指针从“const MyClass”转换为“MyClass &”
嘿嘿原来是这样:当我们定义了一个类的成员函数会默认传进来一个this指针,但是如果是一个const 函数那么是不是就传竟来一个const的指针呢?所以我想编译器看到是这样的:
CString MyClass::GetStringValue(const MyClass* this)
{
if(this->m_strValue.IsEmpty())
this->SetStringValue(); return this->GetStringValue();
}
后来验证一下,创建了一个static函数模拟一下:
CString MyClass::GetStringValueS(const MyClass* mcp)
{
if(mcp->m_strValue.IsEmpty())
mcp->SetStringValue(); return mcp->GetStringValue();
}
编译真的得到一个同样的错误:
error C2662: “MyClass::SetStringValue”: 不能将“this”指针从“const MyClass”转换为“MyClass &”
所以我试着去打破const规则:
CString MyClass::GetStringValue()const
{
MyClass * nonconstthis = (MyClass *)this;
if(m_strValue.IsEmpty())
nonconstthis->SetStringValue(); return m_strValue;
}
结果编译通过,执行也没有问题。
所以有一个转换const_cast<>专门来做这样的事情:
CString MyClass::GetStringValue()const
{
if(m_strValue.IsEmpty())
const_cast<MyClass *>(this)->SetStringValue(); return m_strValue;
}
最后,我认为,这样违背了const设计的初衷,这样对于调用者是一种欺骗(违背契约:我保证不改你),所以不推荐这样使用。
打破C++ Const 的规则的更多相关文章
- const修饰规则 及其 用法
const指针和指向const变量的指针,在写法上容易让人混淆,记住一个规则:从左至右,依次结合,const就近结合. 比如,int * const p: 1.int * (const p):变量p经 ...
- 【转】CSS样式覆盖规则
大家都知道CSS的全称叫做“层叠样式表”,但估计很多人都不知道“层叠”二字的含义.其实,“层叠”指的就是样式的覆盖,当一个元素被运用上多种样式,并且出现重名的样式属性时,浏览器必须从中选择一个属性值, ...
- (转) C/C++中const关键字详解
文章转自 http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777416.html 为什么使用const?采用符号常量写出的代码更容易维 ...
- c/c++中const使用总结(金典)
原文地址:http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777416.html 个人总结: (1)const只对它左 ...
- c++的const总结
转自:http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777416.html 为什么使用const?采用符号常量写出的代码更容易维护:指 ...
- C#命名规则和风格(收集)
1. 文件命名组织 1-1文件命名 1. 文件名遵从Pascal命名法,无特殊情况,扩展名小写. 2. 使用统一而又通用的文件扩展名: C# 类 .cs 1-2文件 ...
- [C++] CONST 2
The C++ 'const' Declaration: Why & How The 'const' system is one of the really messy features of ...
- CSS样式:覆盖规则
规则一:由于继承而发生样式冲突时,最近祖先获胜. CSS的继承机制使得元素可以从包含它的祖先元素中继承样式,考虑下面这种情况: <html> <head> <title& ...
- const关键字祥解
为什么使用const?采用符号常量写出的代码更容易维护:指针常常是边读边移动,而不是边写边移动:许多函数参数是只读不写的.const最常见用途是作为数组的界和switch分情况标号(也可以用枚举符代替 ...
随机推荐
- mysql命令行执行外部文件
mysql命令行执行外部文件
- Windows 批处理文件
窗口自动关闭:批处理文件执行完之后,窗口会自动关闭,若想执行完之后,窗口不自动关闭的话,在文件末尾添加1. 批处理文件执行完之后,窗口会自动关闭2. 若想执行完之后,窗口不自动关闭的话,在文件末尾添加 ...
- 转:MFC网络编程学习
要学习好网路编程,主要看以下几个方面: 1.掌握概念,诸如:同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)等. 2.在实际Windows网络通信软件开发中,异步非阻 ...
- 专门讲讲这个MYSQL授权当中的with grant option的作用
对象的owner将权限赋予某个用户(如:testuser1) grant select ,update on bd_corp to testuser1 [with grant option ]1.如果 ...
- Web Server (IIS) Administration Cmdlets in Windows PowerShell
https://technet.microsoft.com/en-us/library/ee790599.aspx Web Server (IIS) Administration Cmdlets in ...
- 转:Yii 常量的轻松管理
问题 我经常在不同的地方使用模型中的常量(基本状态常量),当常量改变时我不得不在使用每处它的代码中修改. 获取常量 为了解决这个问题我使用了一个方法 getConstants(). public st ...
- AsyncTask和Handler的优缺点比较
AsyncTask实现的原理和适用的优缺点 AsyncTask,是android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以通过接口 ...
- C# 同步/并发队列ConcurrentQueue (表示线程安全的先进先出 (FIFO) 集合)
http://msdn.microsoft.com/zh-cn/library/dd267265(v=vs.110).aspx static void Main(string[] args) { // ...
- Codeforces - ZeptoLab Code Rush 2015 - D. Om Nom and Necklace:字符串
D. Om Nom and Necklace time limit per test 1 second memory limit per test 256 megabytes input standa ...
- [LeetCode] 203. Remove Linked List Elements 解题思路
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...