记得适当的声明成员函数为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 ...
随机推荐
- LeetCode OJ:Length of Last Word(最后一个词的长度)
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- ZOJ - 3201 Tree of Tree (树形背包)
题意:有一棵树,树上每个结点都有一个权值,求恰好包含k个结点的子树的最大权值. 设dp[i][j]为以结点i为根的树中包含j个结点的子树的最大权值,则可以把这个结点下的每棵子树中所包含的所有子树的大小 ...
- identityservice4使用案例
一 使用缘由 最近写微服务的blog,研读了o’reilly出的 <building Microservices With Asp.net Core>,其中使用的微服务分布式权限组件是mi ...
- js整数千分化
function numToQfh(num){ var num_str = num.toString(); var strTo = " "; while(num_str.lengt ...
- 非maven项目下载maven的jar
很多时候我们需要jar,可惜项目不是maven的,但是我们只有一个maven的坐标,那怎么办? 比如: <dependencies> <dependency> <grou ...
- Azure VM Disk的设计与部署
Azure的VM的设计中,Disk相关的设计是非常重要的一个内容,本文将介绍Azure上的VM的Disk相关的一些最佳实践和一些小的技巧. 一.Azure VM中Disk的存储账户设计 1. Stor ...
- 蓝桥杯 算法训练 ALGO-146 4-2找公倍数
算法训练 4-2找公倍数 时间限制:1.0s 内存限制:256.0MB 查看参考代码 问题描述 这里写问题描述. 打印出1-1000所有11和17的公倍数. 样例输入 一个满足题 ...
- H264系列(9):H264中的时间戳(DTS和PTS)
(1)Ffmpeg中的DTS 和 PTS H264里有两种时间戳:DTS(Decoding Time Stamp)和PTS(Presentation Time Stamp). 顾名思义,前者是解码的时 ...
- java从键盘输入数,分解质因数,
总结:1.break;的用法 当最小质因数不能被输入的值整除时,需要继续循环.k++. 当然输入的数,本身就是质数时,那么 package com.b; import java.util.Scanne ...
- binlog之一:binary log初探
MySQL Binary Log也就是常说的bin-log,是mysql执行改动产生的二进制日志文件,其主要作用有两个: Replication(主从数据库):在master端开启binary log ...