记得适当的声明成员函数为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 ...
随机推荐
- hdoj-1037-Keep on Truckin'(水题)
题目链接 /* 题意:三个通道,如果比168低,那么过不去,输出最先碰到的低的通道高度值 */ #include <iostream> using namespace std; int ...
- BEC listen and translation exercise 12
More than 220 cities now have air quality monitoring systems and 42 others will have systems in plac ...
- 使用open_read_write等底层函数来赋值一个文件
/* * 该程序是练习read(),write(),open(),create(),close(),lseek()等函数. * * 该程序的处理思路: * 1: 在程序所在的目录去打开一个文件,如 ...
- java06-数组动手动脑
1.阅读QiPan.java示例程序了解如何利用二维数组和循环语句绘制五子棋盘. 定义了一个私有的二维数组作为棋盘.并定义了长度.之后打印符号使之连接起来作为棋盘在控制台显示.建立缓冲区用来读取输入的 ...
- ACC 001 C - Shorten Diameter 图论
题目: Problem Statement Given an undirected tree, let the distance between vertices \(u\) and \(v\) be ...
- Node.js + Express
相关链接: 1.https://www.jianshu.com/p/db4df1938eca 2.前端发起GET请求:http://localhost:3000/api/login?name=admi ...
- python basestring()
作用: basestring是str和unicode的超类(父类),也是抽象类,因此不能被调用和实例化,但可以被用来判断一个对象是否为str或者unicode的实例,isinstance(obj, b ...
- dubbo的监控中心
监控中心实际上就是一个web工程. 是dubbo官方给我们提供的一个war包. 只需要部署在Tomcat中就可以了. 推荐把监控中心与注册中心部署在一台服务器上,这样可以不需要任何配置. 部署完成后直 ...
- 免费的xshell下载
平时从网站上下载的xshell,都是有有效期的,用一个月就生效了 所以我们可以这样: 学习源头: https://blog.csdn.net/zhoukikoo/article/details/794 ...
- 第二届PHP全球开发者大会(含大会的PPT)
PHP全球开发者大会于2016年5月14日至15日在北京召开 更多现场图片请猛击: http://t.cn/RqeP7y9 , http://t.cn/RqD8Typ 最后,这次大会的PPT可以在这 ...