The constness of a method should makes sense from outside the object
C++的encapsulation机制使得我们可以使得一个类的逻辑接口和内部表示有很大的差异,比如下面这个矩形类:
class Rectangle
{
public:
int width() const {return x;}
int height() const {return y;}
int Area() const {return x * y;}
int totalLength() const {return * (x + y);}
private:
int x, y;
};
从逻辑接口上看,我们可以认为这个类有四个状态(width(), height(), Area(), totalLength()),尽管实际上我们并不是这么表示的。
有一点特别需要注意的是,当我们决定一个成员函数是不是const的时候,我们考虑的是它会不会导致逻辑状态的变更。
可以考虑这样一个容器类:
template<typename T>
class CacheVector
{
public:
T lookup(int i ) const
{
assert(i >= && i < size);
cache = _data[i];
return _data[i];
}
private:
T* _data;
size_t _size;
mutable T cache;
};
这段代码有两点值得关注:为什么lookup方法被设计成const的?mutable起什么作用?
因为lookup并没有改变CacheVector<T>的逻辑状态,尽管我们更新了缓存,但这个对于类的使用者而言是不可见的。
mutable关键字的作用就是在const成员函数里面修改特定成员,事实上也表明这个成员与类的逻辑状态没有关系。
The constness of a method should makes sense from outside the object的更多相关文章
- Error:(23, 0) Could not find method implementation() for arguments [directory 'libs'] on object of t
Error:(28, 0) Could not find method implementation() for arguments [com.android.support:appcompat-v7 ...
- inside when() you don't call method on mock but on some other object
错误原因:调用静态方法,要事先引入静态类,否则mock的时候会默认为测试的类 解决方法:@PrepareForTest({SecurityContextHolder.class})引入静态类 注:@P ...
- What is the difference between routine , method , procedure , function ? please explain it with example?
a method is named and attached to an object. so, for example, a method is like a function but is con ...
- Core Java Volume I — 4.5. Method Parameters
4.5. Method ParametersLet us review the computer science terms that describe how parameters can be p ...
- A javascript library providing cross-browser, cross-site messaging/method invocation. http://easyxdm.net
easyXDM - easy Cross-Domain Messaging easyXDM is a Javascript library that enables you as a develope ...
- Java 反射机制[Method反射]
Java 反射机制[Method反射] 接着上一篇Java 反射机制[Field反射],通过调用Person类的setName方法将obj的name字段的Value设置为"callPerso ...
- 【java】method.invoke(方法底层所属对象/null,new Object[]{实际参数})
反射调方法时无论是静态/非静态,固定/可变参数,都有Object对象数组对参数进行包装. package com.tn.clas; import java.lang.reflect.Method; i ...
- method.invoke()s
在框架中经常会会用到method.invoke()方法,用来执行某个的对象的目标方法.以前写代码用到反射时,总是获取先获取Method,然后传入对应的Class实例对象执行方法.然而前段时间研究inv ...
- java中Method.invoke方法参数解析
通过发射的机制,可以通过invoke方法来调用类的函数.invoke函数的第一个参数是调用该方法的实例,如果该方法是静态方法,那么可以用null或者用类来代替,第二个参数是变长的,是调用该方法的参数. ...
随机推荐
- 学习ASP.NET之前,先了解它
ASP.NET是一个使用HTML,CSS,JavaScript和服务器脚本构建的网页和网站的开发框架,不是一门编程语言. ASP.NET支持三种不同的开发模式:Web Pages(Web页面),MVC ...
- centos svn服务器安装
1.安装必须的软件 yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql 2.创建代码库 mkdir -p /root ...
- 手机页面head中的meta元素
<meta http-equiv="Pragma" content="no-cache"> <meta http-equiv="ex ...
- 转载:Clear Float
众所周知,平时在写HTML代码时,难免少不了使用Float样式,这样一来,假使您没有清除浮动,那么有浮动元素的父元素容器将元素将无法自动撑 开.换句简单好理解的话来说,假如你在写CODE时,其中div ...
- Storm(2) - Log Stream Processing
Introduction This chapter will present an implementation recipe for an enterprise log storage and a ...
- [转载]Windows 7笔记本创建wifi热点供手机上网教程
用智能手机的朋友会发现这样一个问题,智能手机比普通手机上网更耗流量.这是因为智能手机应用(软件)丰富,而且大部分应用都会自动联网.为此,许多人每月包了上百M的流量套餐,但用的时候还是小心翼翼,生怕流量 ...
- js计算日期的前几天的日期
月份0---11 var date = new Date(year,fenye_arr[0]-1,fenye_arr[1]); miao=date.getTime(); var ...
- iOS Android图标生成器PHP
<?php //修改为你想要的大小 //$sizes = array(16,29,32,36,48,50,57,58,72,76,96,100,114,120,128,144,152); $si ...
- SQL SERVER 创建作业
),, , , , , ),SERVERPROPERTY(N, , , ...
- ros使用RPLIDAR激光雷达
1.首先下载RPLIDAR的驱动功能包 https://github.com/robopeak/rplidar_ros 2.然后解压放到~/catkin_ws/src目录下 3.执行catkin_ma ...