【c++】类中的const成员

简介:

const成员变量

举个例子

复制代码
#include <iostream>
using namespace std;

class A
{
    public:
        A(int size) : SIZE(size) {};
    private:
        const int SIZE;
};

int main() { A a(100); }
复制代码

说明

  • 在类中声明变量为const类型,但是不可以初始化
  • const常量的初始化必须在构造函数初始化列表中初始化,而不可以在构造函数函数体内初始化

但是

 此时的const变量属于具体的一个对象,如何在整个类中都恒定不变呢?

 答案是利用枚举,举例

复制代码
#include <iostream>
using namespace std;

class A
{
    private:
        enum {SIZE = 100};
    public:
        int array[SIZE];
};

int main()
{
    A a;
}
复制代码

枚举常量不会占据对象的存储空间,在编译时被全部求值

但是,它隐含的数据对象类型为整形,不能表示其他类型。

问题

如何定义在类中定义非整形常量?(待解决)

 

话说有几个地方必须在构造函数的初始化列表中(详见):

  1. 类的const常量
  2. 类的引用类型成员
  3. 没有默认构造函数的类类型成员
  4. 如果类存在继承关系,派生类必须在其初始化列表中调用基类的构造函数

 

const成员函数

任何不修改数据成员的函数都应该声明为const类型。如果在编写const成员函数时,不慎修改了数据成员,或调用了其他非const成员函数,编译器就会指出错误。

示例说明

复制代码
#include <iostream>
using namespace std;
class Stack
{
    public:
        void Push(int item);
        int Pop(void);
        int GetCount(void) const;
    private:
        int m_num; 
        int m_data[100];
};

int Stack::GetCount(void) const
{
    ++m_num;     //编译错误,企图修改数据成员
    Pop();       //编译错误,企图调用非const函数
    return m_num;
}
复制代码

 

同一个类中,可以仅通过是否是const定义两个函数名字、参数、返回值完全相同的两个成员函数,例如:

复制代码
#include <iostream>
using namespace std;

class A
{
    public:
        A(int v): val(v) {}
        void print_val() { cout << "not const:" << val << endl;}
        void print_val() const { cout << "const print_val:" << val << endl;}
    private:
        int val;
};
int main(int argc ,char **argv)
{
    A b(45);
    b.print_val();

    const A a(12);
    a.print_val();
}
复制代码

输出

结论

同函数名、参数、返回值可以仅通过是否为const来定义为类的两个成员函数。在调用时,const对象调用const成员函数,非const对象调用非const成员函数。

问题 1:

不可以在const函数中改变成员变量的值,那么有没有办法改变?

答案是可以的,把成员变量声明为mutable类型。看程序

复制代码
#include <iostream>
using namespace std; class A { public: A(int v): val(v) {} void print_val() { cout << "not const:" << val << endl;} void print_val() const { val++; cout << "const print_val:" << val << endl;} private: mutable int val; }; int main(int argc ,char **argv) { A b(45); b.print_val(); const A a(12); a.print_val(); }
复制代码

结果

 

问题2:

当类中只有const函数,非const对象是否可以调用const函数?

答案是可以的,程序

复制代码
#include <iostream>
using namespace std; class A { public: A(int v): val(v) {} // void print_val() { cout << "not const:" << val << endl;} void print_val() const { val++; cout << "const print_val:" << val << endl;} private: mutable int val; }; int main(int argc ,char **argv) { A b(45); b.print_val(); const A a(12); a.print_val(); }
复制代码

结果

但是:还有非const函数时,非const对象不可以调研那个const函数(否则,类的数据变量就会发生变化)。

 

问题3:

当类中存在只有 是否为const 不同的两个函数时,const函数是否可以暂时调用那个非const函数?

答案是可以的。用const_cast将转化掉表达式的const性质

复制代码
#include <iostream>
using namespace std;

class A
{
    public:
        A(int v): val(v) {}
        void print_val() { cout << "not const:" << val << endl;}
        void const print_val() const { cout << "const print_val:" << val << endl;}
    private:
        int val;
};
int main(int argc ,char **argv)
{
    A b(45);
    b.print_val();

    const A *a = new A(45);
    const_cast<A*>(a)->print_val();
    a->print_val();
}
复制代码

结果

注意

单纯用类转化不行

const A a(45);
const_cast<A> a.print_val();

报错

问题4:返回类型是const是怎么回事?

const返回类型只有在修饰指针或引用是才有用。单凭const返回类型不可以重载。





本文转自jihite博客园博客,原文链接:http://www.cnblogs.com/kaituorensheng/p/3244910.html,如需转载请自行联系原作者


相关文章
|
4天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
19 0
|
4天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
19 0
|
2天前
|
设计模式 Java C++
【C++高阶(八)】单例模式&特殊类的设计
【C++高阶(八)】单例模式&特殊类的设计
|
2天前
|
编译器 C++
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
|
7天前
|
存储 安全 C语言
【C++】string类
【C++】string类
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
8天前
|
编译器 C++
标准库中的string类(上)——“C++”
标准库中的string类(上)——“C++”
|
28天前
|
存储 C++ 容器
C++入门指南:string类文档详细解析(非常经典,建议收藏)
C++入门指南:string类文档详细解析(非常经典,建议收藏)
38 0
|
28天前
|
存储 编译器 C语言
C++入门: 类和对象笔记总结(上)
C++入门: 类和对象笔记总结(上)
34 0
|
8天前
|
编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”