记得适当的声明成员函数为const.
如果确信一个成员函数不用修改它的对象,就可以声明它为const,这样就可以作用于他的const对象了.因为const对象只能调用它的const方法.
template<class T> class Vector
{
public:
int length() const//如果这里没有const,那么该程序会运行失败,因为padded_length的方法中的参数是const的.
{
return ;
};
int length(int);
};
template<class T>
int padded_length(const Vector<T>&v,int n)
{
int k = v.length();
return k>n?k:n;
}
int main(int argc,char* argv[])
{
Vector<int> a;
cout<<padded_length(a,)<<endl;
return ;
}
记得适当的声明成员函数为const.的更多相关文章
- const成员函数和const对象
从成员函数说起 在说const成员函数之前,先说一下普通成员函数,其实每个成员函数都有一个隐形的入参:T *const this. int getValue(T *const this) { retu ...
- 成员函数的const到底修饰的是谁
demo <pre name="code" class="cpp">class Test { public: const void OpVar(in ...
- 成员函数的const究竟修饰的是谁
demo <pre name="code" class="cpp">class Test { public: const void OpVar(in ...
- 成员函数的const不能被修改,包括指针
#include <iostream> class A { private: std::string a; public: A(std::string b) :a(b){} const c ...
- C++中的const成员函数(函数声明后加const,或称常量成员函数)用法详解
http://blog.csdn.net/gmstart/article/details/7046140 在C++的类定义里面,可以看到类似下面的定义: 01 class List { 02 priv ...
- c++ const 成员函数
第一个事实: 某类中可以这么声明定义两个函数,可以重载(overload) void pa(){ cout<<"a"<<endl; } void pa() ...
- c++ 学习之const专题之const成员函数
一些成员函数改变对象,一些成员函数不改变对象. 例如: int Point::GetY() { return yVal; } 这个函数被调用时,不改变Point对象,而下面的函数改变Point对象: ...
- C++ Const成员函数
一些成员函数改变对象,一些成员函数不改变对象. 例如: int Point::GetY() { return yVal; } 这个函数被调用时,不改变Point对象,而下面的函数改变Point对象 ...
- c++中的const参数,const变量,const指针,const对象,以及const成员函数
const 是constant 的缩写,“恒定不变”的意思.被const 修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性.所以很多C++程序设计书籍建议:“Use const whe ...
随机推荐
- Android 禁止屏幕旋转、避免转屏时重启Activity
一.禁止屏幕旋转 在AndroidManifest.xml的每一个需要禁止转向的Activity配置中加入android:screenOrientation属性: 可选项: landscape = 横 ...
- 以前的实验博客地址,以前使用csdn
实验一木马分析(隐藏分析)实验 http://blog.csdn.net/qq_30600405/article/details/78360599 实验二木马分析(控制分析)实验和实验三冰河木马实验 ...
- 前端用户输入校验--基于JQ
<!DOCTYPE html> <html> <head> <title>用户输入校验</title> </head> < ...
- BZOJ4130:[PA2011]Kangaroos
浅谈\(K-D\ Tree\):https://www.cnblogs.com/AKMer/p/10387266.html 题目传送门:https://lydsy.com/JudgeOnline/pr ...
- [转载]Linux驱动-SPI驱动 之二:SPI通用接口层
通过上一篇文章的介绍,我们知道,SPI通用接口层用于把具体SPI设备的协议驱动和SPI控制器驱动联接在一起,通用接口层除了为协议驱动和控制器驱动提供一系列的标准接口API,同时还为这些接口API定义了 ...
- 虚幻引擎4设置Visual Studio
转自:http://www.unrealchina.net/portal.php?mod=view&aid=149 设置Visual Studio和虚幻引擎4协同工作有利于提高开发人员使用UE ...
- app中使用微信分享注意事项
1. 在微信公众平台开通一个微信公众号,https://mp.weixin.qq.com 2. 将自己制作好的已签名的app安装到手机上 3. 下载微信开放平台获取应用签名的apk--- gen ...
- 开源JS代码前面加!,+的意义
我们都知道,函数的声明方式有这两种 function fnA(){alert('msg');}//声明式定义函数 var fnB = function(){alert('msg');}//函数赋值表达 ...
- Windows Backdoor Tips
名称:在用户登录时,运行这些程序 位置: Computer Configuration\\Policies\\Administrative Templates\\System\\Logon\\ 中 d ...
- suse 源的添加与删除,以及源地址
地址 一个是上海交大的,http://ftp.sjtu.edu.cn/opensuse/update/ 葡萄牙的: http://ftp.nux.ipb.pt/pub/dists/opensuse/u ...