6. C++ 中的const

在类中,如果你不希望某些数据被修改,可以使用const关键字加以限定。const 可以用来修饰成员变量和成员函数。

6.1 const成员变量

const 成员变量只需要在声明时加上 const 关键字。
初始化 const 成员变量只有一种方法,就是通过构造函数的初始化列表。


6.2 const成员函数(常成员函数)

const 成员函数可以使用类中的所有成员变量,但是不能修改它们的值,这种措施主要还是为了保护数据而设置的。
我们通常将 get 函数设置为常成员函数。读取成员变量的函数的名字通常以get开头,后跟成员变量的名字,所以通常将它们称为 get 函数。
常成员函数需要在声明和定义的时候在函数头部的结尾加上 const 关键字,例如:

#include <iostream>
using namespace std;

class Student{
public:
    Student(char *name, int age, float score);
    void show();
    //声明常成员函数
    char *getname() const;
    int getage() const;
    float getscore() const;
private:
    char *name;
    int age;
    float score;
};

Student::Student(char *name, int age, float score): name(name), age(age), score(score){ }
void Student::show(){
    cout<<name<<"的年龄是"<<age<<",成绩是"<<score<<endl;
}

//定义常成员函数
char * Student::getname() const{
    return name;
}

int Student::getage() const{
    return age;
}

float Student::getscore() const{
    return score;
}

int main() {
    return 0;
}

必须在成员函数的声明和定义处同时加上 const 关键字。

char *getname() const
char *getname()

是两个不同的函数原型,如果只在一个地方加 const 会导致声明和定义处的函数原型冲突。

注意:
1、函数开头的 const 用来修饰函数的返回值,表示返回值是 const 类型,也就是不能被修改,如const char * getname()。
2、函数头部的结尾加上 const 表示常成员函数,这种函数只能读取成员变量的值,而不能修改成员变量的值,如char * getname() const。


6.3 const对象(常对象)

定义常对象的语法和定义常量的语法类似:

const  类名  对象名(参数);
类名  const 对象名(参数);

当然你也可以定义 const 指针:

const 类名 *变量 = new 类名(参数);
类名 const *变量 = new 类名(参数);

实例

#include <iostream>
using namespace std;

class Student{
public:
    Student(char *name, int age, float score);
public:
    void show();
    char *getname() const;
    int getage() const;
    float getscore() const;
private:
    char *name;
    int age;
    float score;
};

Student::Student(char *name, int age, float score): name(name), age(age), score(score){ }
void Student::show(){
    cout<<name<<"的年龄是"<<age<<",成绩是"<<score<<endl;
}

char * Student::getname() const{
    return name;
}

int Student::getage() const{
    return age;
}

float Student::getscore() const{
    return score;
}

int main(){
    const Student stu("豆豆", 12, 85.0);
    //stu.show(); //error
    cout<<stu.getname()<<"的年龄是"<<stu.getage()<<",成绩是"<<stu.getscore()<<endl;
    const Student *pstu = new Student("哈哈", 16, 65.0);
    //pstu -> show(); //error
    cout<<pstu->getname()<<"的年龄是"<<pstu->getage()<<",成绩是"<<pstu->getscore()<<endl;
    return 0;
}

![image.png](https://upload-images.jianshu.io/upload_images/16823531-8f0356afab36b6a6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

一旦将对象定义为常对象之后,该对象就只能访问被 const 修饰的成员了(包括 const 成员变量和 const 成员函数),因为非 const 成员可能会修改对象的数据(编译器也会怀疑)。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 前言 把《C++ Primer》[https://book.douban.com/subject/25708312...
    尤汐Yogy阅读 9,618评论 1 51
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,530评论 1 32
  • 总体上必须清楚的:1)程序结构是三种: 顺序结构 , 循环结构(三个循环结构), 选择结构(if 和 switch...
    静熙老师哈哈哈阅读 891评论 1 20
  • C++文件 例:从文件income. in中读入收入直到文件结束,并将收入和税金输出到文件tax. out。 检查...
    SeanC52111阅读 3,054评论 0 3
  • 爱上走路, 就是选择这样一种运动: 既不与跑者争速度, 也不在踱方步里享受从容, 向前去, 让脚步一刻不停。 爱上...
    凿冰而钓阅读 476评论 0 1

友情链接更多精彩内容