zoukankan      html  css  js  c++  java
  • 类中的const使用

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

    const成员变量

    const 成员变量的用法和普通 const 变量的用法相似,只需要在声明时加上 const 关键字。初始化 const 成员变量只有一种方法,就是通过参数初始化表,这点在前面已经讲到了,请猛击《C++构造函数的参数初始化表》回顾。

    const成员函数

    const 成员函数可以使用类中的所有成员变量,但是不能修改它们的值,这种措施主要还是为了保护数据而设置的。const 成员函数也称为常成员函数

    常成员函数需要在声明和定义的时候在函数头部的结尾加上 const 关键字,请看下面的例子:
    1. class Student{
    2. public:
    3. Student(char *name, int age, float score);
    4. void show();
    5. //声明常成员函数
    6. char *getname() const;
    7. int getage() const;
    8. float getscore() const;
    9. private:
    10. char *m_name;
    11. int m_age;
    12. float m_score;
    13. };
    14. Student::Student(char *name, int age, float score): m_name(name), m_age(age), m_score(score){ }
    15. void Student::show(){
    16. cout<<m_name<<"的年龄是"<<m_age<<",成绩是"<<m_score<<endl;
    17. }
    18. //定义常成员函数
    19. char * Student::getname() const{
    20. return m_name;
    21. }
    22. int Student::getage() const{
    23. return m_age;
    24. }
    25. float Student::getscore() const{
    26. return m_score;
    27. }
    getname()、getage()、getscore() 三个函数的功能都很简单,仅仅是为了获取成员变量的值,没有任何修改成员变量的企图,所以我们加了 const 限制,这是一种保险的做法,同时也使得语义更加明显。

    需要注意的是,必须在成员函数的声明和定义处同时加上 const 关键字。char *getname() constchar *getname()是两个不同的函数原型,如果只在一个地方加 const 会导致声明和定义处的函数原型冲突。

    const对象

    const 也可以用来修饰对象,称为常对象。一旦将对象定义为常对象之后,就只能调用类的 const 成员了。

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

    const  class  object(params);
    class const object(params);

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

    const class *p = new class(params);
    class const *p = new class(params);

    class为类名,object为对象名,params为实参列表,p为指针名。两种方式定义出来的对象都是常对象。
    如果你对 const 的用法不理解,请猛击《C语言const:禁止修改变量的值》。
    一旦将对象定义为常对象之后,不管是哪种形式,该对象就只能访问被 const 修饰的成员了(包括 const 成员变量和 const 成员函数),因为非 const 成员可能会修改对象的数据(编译器也会这样假设),C++禁止这样做。

    常对象使用举例:
    1. #include <iostream>
    2. using namespace std;
    3. class Student{
    4. public:
    5. Student(char *name, int age, float score);
    6. public:
    7. void show();
    8. char *getname() const;
    9. int getage() const;
    10. float getscore() const;
    11. private:
    12. char *m_name;
    13. int m_age;
    14. float m_score;
    15. };
    16. Student::Student(char *name, int age, float score): m_name(name), m_age(age), m_score(score){ }
    17. void Student::show(){
    18. cout<<m_name<<"的年龄是"<<m_age<<",成绩是"<<m_score<<endl;
    19. }
    20. char * Student::getname() const{
    21. return m_name;
    22. }
    23. int Student::getage() const{
    24. return m_age;
    25. }
    26. float Student::getscore() const{
    27. return m_score;
    28. }
    29. int main(){
    30. const Student stu("小明", 15, 90.6);
    31. //stu.show(); //error
    32. cout<<stu.getname()<<"的年龄是"<<stu.getage()<<",成绩是"<<stu.getscore()<<endl;
    33. const Student *pstu = new Student("李磊", 16, 80.5);
    34. //pstu -> show(); //error
    35. cout<<pstu->getname()<<"的年龄是"<<pstu->getage()<<",成绩是"<<pstu->getscore()<<endl;
    36. return 0;
    37. }
    本例中,stu、pstu分别是常对象以及常对象指针,它们都只能调用 const 成员函数。
  • 相关阅读:
    520了,用32做个简单的小程序
    年轻就该多尝试,教你20小时Get一项新技能
    自定义注解!绝对是程序员装逼的利器!!
    vs2015添加ActiveX Control Test Container工具(转载)
    编译MapWinGis
    C#遍历集合与移除元素的方法
    c#winform程序,修改MessageBox提示框中按钮的文本
    java程序员面试答题技巧
    什么是DOM
    uml类关系
  • 原文地址:https://www.cnblogs.com/wei1/p/9582136.html
Copyright © 2011-2022 走看看