////////////////////////////////////////////////////////////////////////////////////////
//
// const对象只能调用const this函数。
// 非const 对象可以调用const this函数和非const this函数
//
// void display() const : 编译器将该函数变形为: void display( const Type* this ) ;
// ( const Type* 指向的对象是不能改变的 )
//
// void display() : 编译器将该函数变形为: void display( Type* this ) ;
//
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////
#include <iostream>
using namespace std ;
class Test
{
public:
Test() { }
void display() { cout << "dislapy()" << endl ; }
void display() const { cout << "display() const" << endl ; }
} ;
int main()
{
Test t1 ;
const Test t2 ;
t1.display() ;
t2.display() ;
return 0 ;
}
C++中的const数据成员和cons成员函数
最新推荐文章于 2025-09-22 22:34:20 发布
本文介绍了C++中const对象与非const对象的区别,以及如何使用const成员函数。通过示例展示了const对象只能调用const类型的成员函数,而非const对象则可以调用所有类型成员函数的特点。
8437

被折叠的 条评论
为什么被折叠?



