记得适当的声明成员函数为const.

如果确信一个成员函数不用修改它的对象,就可以声明它为const,这样就可以作用于他的const对象了.因为const对象只能调用它的const方法.

 1 template<class T> class Vector
 2 {
 3 public:
 4     int length() const//如果这里没有const,那么该程序会运行失败,因为padded_length的方法中的参数是const的.
 5     {
 6         return 2;
 7     };
 8     int length(int);
 9 };
10 template<class T>
11 int padded_length(const Vector<T>&v,int n)
12 {
13     int k = v.length();
14     return k>n?k:n;
15 }
16 int main(int argc,char* argv[])
17 {
18     Vector<int> a;
19     cout<<padded_length(a,3)<<endl;
20     return 0;
21 }

记得适当的声明成员函数为const.

时间: 2024-08-29 08:17:36

记得适当的声明成员函数为const.的相关文章

【03】指针、返回值、成员函数的const

1 // 2 // 2015-03-05 03/55 3 // 指针.返回值.成员函数 之 const 4 // 5 6 #include <iostream> 7 #include <string> 8 9 using namespace std; 10 11 // ------------------------------ 12 // 1. 指针的const 13 // ------------------------------ 14 const char *p1 = &q

C++ Primer 学习笔记_24_类与数据抽象(10)--static 与单例模式、auto_ptr与单例模式、const成员函数、const 对象、mutable修饰符

C++ Primer 学习笔记_24_类与数据抽象(10)--static 与单例模式.auto_ptr与单例模式.const成员函数.const 对象.mutable修饰符 前言 [例]写出面向对象的五个基本原则? 解答:单一职责原则,开放封闭原则,依赖倒置原则,接口隔离原则和里氏替换原则 里氏替换原则:子类型必须能够替换他们的基类型. 设计模式分为三种类型:创建型模式.结构型模式和行为型模式 一.static 与单例模式 1.单例模式 单例模式的意图:保证一个类仅有一个实例,并提供一个访问它

成员函数的const到底修饰的是谁

demo <pre name="code" class="cpp">class Test { public: const void OpVar(int a, int b) { a = 100; } protected: private: }; Test类中有一个成员函数OpVar,有一个const修饰符也可以写成下两种形式 class Test { public: void const OpVar(int a, int b) { a = 100; } p

const成员函数和const对象的调用关系

代码: class A{ private: int a; public: void set() { } <span style="white-space:pre"> </span>void set () const <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>} }

成员函数的const不能被修改,包括指针

#include <iostream> class A { private: std::string a; public: A(std::string b) :a(b){} char& operator[](int b)const { return a[b]; } }; int main() { A a("hello"); //a[0]='j'; 不能 char*p = &a[0]; *p = 'j'; 也不能,编译器信息:“return”: 无法从“con

拷贝构造函数和const成员函数

实验原因 说明如何使用const描述保护类数据不会意外修改. 编译环境 vc6sp6 + win7x64 工程下载 copyConstruction_constMemberFunction.zip 使用非const成员函数,引起的拷贝构造函数报错 [cpp] view plain copy class CStudent { /// 常量定义 public: enum {NAME_SIZE_MAX = 64}; /// 构造, 拷贝构造, 析构函数 public: CStudent(); CStu

const修饰函数参数 const修饰函数返回值 const修饰成员函数

看到const 关键字,C++程序员首先想到的可能是const 常量.这可不是良好的条件反射.如果只知道用const 定义常量,那么相当于把火药仅用于制作鞭炮.const 更大的魅力是它可以修饰函数的参数.返回值,甚至函数的定义体. const 是constant 的缩写,"恒定不变"的意思.被const 修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性.所以很多C++程序设计书籍建议:"Use const whenever you need". 1

c++中的const参数,const变量,const指针,const对象,以及const成员函数

const 是constant 的缩写,“恒定不变”的意思.被const 修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性.所以很多C++程序设计书籍建议:“Use const whenever you need”. 1.用const 修饰函数的参数 如果参数作输出用,不论它是什么数据类型,也不论它采用“指针传递”还是“引用传递”,都不能加const 修饰,否则该参数将失去输出功能.const 只能修饰输入参数: 如果输入参数采用“指针传递”,那么加const 修饰可以防止意外地

C++中const对象和const成员函数

1?  成员函数可以声明成const函数(声明后加const) 2?  对于const对象,只能调用const成员函数 3?  Const函数和非const函数可以形成重载 4?  对于非const对象的函数调用优先选择非const成员函数 5?  对于类中的mutable数据成员,可以被const成员函数修改 // // main.cpp // Const // // Created by 06 on 15/1/24. // Copyright (c) 2015年 黄永锐. All right