您的位置:首页 > 编程语言 > C语言/C++

C++中的const数据成员和cons成员函数

2013-10-18 20:10 225 查看
////////////////////////////////////////////////////////////////////////////////////////
//
//  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 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: