1.成员变量和成员函数分开储存

只有非静态成员变量才属于类的对象上。

空对象内存占用空间为1


this指针的概念

this指针指向被调用的成员函数所属的对象

this指针式隐含每个非静态成员函数内的一种指针。

this指针不需要定义,直接使用即可。

this指针的用途:

1.当形参和成员变量同名时,可用this指针来区分。

2.在类的非静态成员函数中返回对象本身,可使用return  *this。


空指针调用成员函数

例子:

#include <iostream>

using namespace std;


//空指针调用成员函数

class person

{

public:

void showperson()

{

 cout << "this is person class" << endl;

}


void showage()

{

 if (this == NULL)

  return;

 //报错原因是因为传入指针是NULL

 cout << this->m_age << endl;

}

int m_age;

};


void test()

{

person* p = NULL;

p->showperson();

p->showage();

}


int main()

{

test();

system("pause");

return 0;

}


const修饰成员函数

常函数:

1.成员函数后加const后我们称为这个函数为常函数

2.常函数内不可以修改成         员属性。

3.成员属性声明时加关键字mutable后,在常函数中依然可以修改。

常对象:

1.声明对象前加const称为该对象为常对象。

2.常对象只能调用常函数。