static:
1–类的静态成员为其所有对象共享,不管有多少对象,静态成员只有一份存于公用内存中。
2–静态函数或者静态的方法都是不存在This指针的,重点就在这个This指针上面。
3–普通方法可以调用静态方法;但是静态方法没法访问普通方法。
const:
void fun(const Test &t)const
{…}
第一个const是保护所传的参数不被修改,第二个const是说明该方法是常方法,常方法表示在这个方法或者函数的内部的数据成员进行修改。
/
如果一个方法不需要改变其参数,那咱们将其设置成常方法,反之设置成普通方法;
一般方法可以调用常方法;
常方法无法调用一般方法。
int a=10;
const int &b=a;
定义一个变量a我们可以用不可改变的b去引用a
const int a=10;
int &b=a;
定义一个常量a我们不可以用变量去引用a。
附代码:
#include<iostream>
using namespace std;
class Test
{
friend void fun(Test &);
public:
Test(int d=0):data(d)
{
count++;
}
~Test()
{
count--;
}
//void print(const Test*const this)
void print()const
{
show();
}
public:
//void list(Test*const this)
void list()
{
cout<<"This is list()"<<endl;
data=0;
}
//void list(const Test*const this)
void list()const//常方法
{
cout<<"This is list()const"<<endl;
//data=0;//此处当改变data的值时会出现编译错误
}
public:
int GetData()const
{
return data;
}
private:
int data;
static int count;
};
int Test::count=0;//对count 进行初始化
void fun(Test &t)
{
cout<<"t.data="<<t.data<<endl;
cout<<"object count="<<Test::count<<endl;
}
void main()
{
Test t(100);
Test t1(200);
fun(t);
}