Something trivial. Type alias and const modifier
2015-11-25 06:41
387 查看
I found something interesting when I was writing a simple homework.
Please take care of this sentence:
Nothing special at first glance. Yet have you noticed the parameter type:
Yes, it is modified by a 'const'. How are we able to change the content anyway?
Well, the reason is that, 'const ListType &target' is different from 'const Node *&target'. The first one is a 'top-level const', i.e. the pointer itself is const. The latter means that the content pointed by the pointer is const, aka a 'low-level const'.
Typically we are not allowed to pass a rvalue to a non-const parameter, because after all it's allowed and very likely not our desire to change the content of a rvalue. That's why we need a const modifier. Yet a type alias breaks the rule to some extent,
for here we are able to change the content anyway. That's the reason why I don't prefer type alias.
One more thing.
int *a = new int(0); const int *b = a; //OK
const int *&c = a; //WRONG, the compiler complains that
on-const lvalue reference to type
'const int *' cannot bind to a value of unrelated type
'int *'
struct Node { string key; int value; Node *next; Node(const string &theKey, int theValue):key(theKey), value(theValue), next(nullptr){} Node(const string &theKey, int theValue, Node *n):key(theKey), value(theValue), next(n){} }; typedef Node * ListType; ListType listFind(const ListType &list, const string &target) { if(!list) return NULL; ListType cur = list; while(cur) { if(cur->key == target) return cur; cur = cur->next; } return NULL; } bool listInsert(ListType &list, const ListType &target) { if(!listFind(list, target->key)) { target->next = list;</span> list = target; return true; } return false; } void listPrint(const ListType &list, ostream &out) { ListType cur = list; while (cur) { out << cur->key << ":" << cur->value << ' '; cur = cur->next; } out << endl; } int main() { Node *A = new Node("Melody", 100); listInsert(A, new Node("Luo", 99)); listPrint(A, cout); return 0; }
Please take care of this sentence:
target->next = list;
Nothing special at first glance. Yet have you noticed the parameter type:
const ListType &target
Yes, it is modified by a 'const'. How are we able to change the content anyway?
Well, the reason is that, 'const ListType &target' is different from 'const Node *&target'. The first one is a 'top-level const', i.e. the pointer itself is const. The latter means that the content pointed by the pointer is const, aka a 'low-level const'.
Typically we are not allowed to pass a rvalue to a non-const parameter, because after all it's allowed and very likely not our desire to change the content of a rvalue. That's why we need a const modifier. Yet a type alias breaks the rule to some extent,
for here we are able to change the content anyway. That's the reason why I don't prefer type alias.
One more thing.
int *a = new int(0); const int *b = a; //OK
const int *&c = a; //WRONG, the compiler complains that
on-const lvalue reference to type
'const int *' cannot bind to a value of unrelated type
'int *'
相关文章推荐
- 两种方法:VS2008下C++窗体程序显示控制台的方法——在QT程序中使用cout和cin
- 把自定义控件集成到Qt Designer中
- uboot - 启动流程分析【第二阶段】
- 单元测试的意义
- 中国省市 JS代码
- 未来WEB程序员
- 国家哀悼日将网站全部变成灰色的代码
- git
- 404页面 3秒后跳到首页 实现
- ASCII码表
- onSaveInstanceState和onRestoreInstanceState
- 常用的CSS命名规则
- Java I/O
- Qt-4.6动画Animation快速入门三字决
- uboot - 启动流程分析【第一阶段】
- 标签中的rel属性的含义
- 用python实现一个简单的计算器
- WCF 自托管、无配置文件实现jsonp(跨域)的访问
- 223. Rectangle Area
- mysql改为mysqli几项注意