1 成员变量和成员函数分开存储
只有非静态成员变量才属于类的对象上
class Person
{
int m_A;//非静态成员变量,属于类的对象上
static int m_B;//静态成员变量,不属于类对象上
void func(){}//非静态成员函数,不属于类对象上
static void func2(){}//静态成员函数,不属于类对象上
}
int main()
{
Person p;
cout<<sizeof(p)<<endl;//4
system("pause");
return 0;
}
注意:空对象占用内存空间为:1;这是因为C++编译器会给每个对象也分配一个字节空间,是为了区分空对象占内存的位置。
2 this指针概念
this指针指向被调用的成员函数所属的对象
this指针是隐含每一个非静态成员函数内的一种指针
this指针不需要定义,直接使用即可
this指针的用途:
- 当形参和成员变量同名时,可用this指针来区分
- 在类的非静态成员函数中返回对象本身,可使用return *this
class Person
{
public:
Person(int age)
{
this->age=age;
}
Person& Personaddage(Person &p)//返回是以引用的方式传递
{
this->age+=p.age;
return *this;
}
int age;
}
int main()
{
Person p1(10);
cout<<p1.age<<endl;//10
Person p2(10);
p2.Personaddage(p1).Personaddage(p1).Personaddage(p1);
cout<<p2.age<<endl;//40
system("pause");
return 0;
}
注:this指针的本质是指针常量,指针的指向是不允许被修改的
3 空指针访问成员函数
C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针
如果用到this指针,需要加以判断保证代码的健壮性
class Person
{
public:
void showclassName(){
cout<<"this is Person class"<<endl;
}
void showPersonAge(){
if(this==nullptr){
return;
}
cout<<"age= "<<m_Age<<endl;
//等价于cout<<"age= "<<this->m_Age<<endl;
}
int m_Age;
}
void test01()
{
Person* p=nullptr;
p->showclassName();//可以正常运行
p->showPersonAge();//如果不加if判断语句,程序就会崩溃
}
int main()
{
test01();
system("pause");
return 0;
}
4 const修饰成员函数
常函数:
- 成员函数后加const后我们称这个函数为常函数
- 常函数内不可以修改成员属性
- 成员属性声明时加关键字mutable后,在常函数中依然可以修改
常对象:
- 声明对象前加const称该对象为常对象
- 常对象只能调用常函数
class Person
{
public:
int m_A;
mutable int m_B;//特殊对象,即使在常函数中,也可以修改这个值,但需要加关键字mutable
void ShowPerson() const//常函数;在成员函数后加const,修饰的是this指针,让指针指向的值也不可 以修改,相当于const Person * const this
{
this->m_B=100;//可以
this->m_A=100;//错误,常函数内不可以修改成员属性
}
void func(){
m_A=100;//可以
}
}
void test01()
{
Person p1;
p1.ShowPerson();//this指针指向p1
}
void test02()
{
const Person p2;//常对象
p2.m_A=100;//错误
p2.m_B=100;//可以,m_B是特殊值,在常对象下也可以修改
p2.ShowPerson();//常对象只能调用常函数
p2.func();//错误,常对象,不可以调用普通成员函数,因为普通成员函数可以修改属性,而常对象不可以修改属性
}
int main(){
test01();
text02();
system("pause");
return 0;
}