34類成員函數(shù)的存儲(chǔ)
在對(duì)象調(diào)用函數(shù)的同時(shí)净当,向該函數(shù)傳遞了該對(duì)象的指針品山,該指針就是this
36const 修飾符
#include<iostream>
using namespace std;
#if 0
1.const 修飾數(shù)據(jù)成員 成員函數(shù) 類對(duì)象
2.修飾數(shù)據(jù)成員時(shí)候,初始化位置只能在參數(shù)列表里面
被const 修飾的數(shù)據(jù)成員 不能被修改
3.修飾成員函數(shù)
位置函數(shù)聲明之后 實(shí)現(xiàn)體之前 要求在聲明和定義處都要有const 修飾
意義:const 函數(shù)承諾 不會(huì)修改數(shù)據(jù)成員
能訪問const和非const數(shù)據(jù)成員 但不能修改 非const 數(shù)據(jù)成員
只能訪問const成員函數(shù)
構(gòu)成重載
const 對(duì)象只能調(diào)用const 成員函數(shù)
非 const對(duì)象 優(yōu)先調(diào)用非const成員函數(shù) 若無 則可調(diào)用const成員函數(shù)
4.修飾類對(duì)象
const 修飾函數(shù) 是從函數(shù)的層面 不修改數(shù)據(jù)
const 修飾對(duì)象 是從對(duì)象的層面 不修改數(shù)據(jù)//只能調(diào)用const 成員函數(shù)
#endif
class A
{
public:
A(int v)
:val(V)
{
}
void dis() const
{
cout<<val<<endl;
//x=200;這個(gè)不可以
//print();這個(gè)也不可以
cout<<"void dis() const"<<endl
}
void dis()
{
cout<<val<<endl;
cout<<"void dis() "<<endl
}
void print()
{
x=100;
y=200牲尺;
}
private:
const int val;//const int val = 10 可以但是不好
int x,y;
}
int main()
{
const A a(5);
a.dis();
return 0;
}