1. 成员变量和成员函数分开存储
在C++中,类内的成员变量和成员函数分开存储
只有非静态成员变量才属于类的对象
#include<iostream>
#include<string>
using namespace std;
//成员变量和成员函数分开存储
//人类
class Person
{
int m_A; //非静态成员变量,属于类的对象
static int m_B; //静态成员变量,不属于类的对象
void func(){} //非静态成员函数,不属于类的对象
static void func2(){} //静态成员函数,不属于类的对象
};
int Person::m_B = 0;
void test01()
{
Person p;
//空对象占用内存空间为:1
//C++编译器会给每个空对象也分配一个字节空间,是为了区分空对象占内存的位置
//每个空对象也应该有一个独一无二的内存地址
cout << "size of p = " << sizeof(p) << endl;
}
int main()
{
test01();
return 0;
}
输出:
---------------------------------------------------------------------------------
size of p = 4
2. this指针
在C++中,类内的成员变量和成员函数分开存储
每一个非静态成员变量只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码
那么问题是:这一块代码是如何区分是哪个对象调用的自己呢?
C++通过提供特殊的对象指针——this指针,解决上述问题。this指针指向被调用的成员函数所属的对象
this指针是隐含每一个非静态成员函数内的一种指针
this指针不需要定义,直接使用即可
this指针的用途:
-
当形参和成员变量同名时,可用this指针来区分
-
在类的非静态成员变量中返回对象本身,可以使用return *this
示例:
#include<iostream>
#include<string>
using namespace std;
//this指针的使用
//人类
class Person
{
public:
Person(int age)
{
//this指针指向被调用的成员函数所属的对象
//当形参和成员变量同名时,this指针可以用来区分
this->age = age;
}
//用引用的方式(Person&)返回,返回的是p2这个对象本身
//如果以返回值的方式进行返回,拷贝构造函数会生成一个新的变量p2'储存p2的值,并返回这个新的变量
Person& PersonAddAge(Person &p)
{
//this指针指向被调用的成员函数所属的对象
this->age += p.age;
//this指向p2的指针,而*this指向p2这个对象本身
return *this;
}
int age;
};
//1.解决名称冲突
void test01()
{
Person p1(18);
cout << "p1的年龄为:" << p1.age << endl;
}
//2.返回对象本身
void test02()
{
Person p1(10);
Person p2(10);
//链式编程思想
p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
cout << "p2的年龄为:" << p2.age << endl;
}
int main()
{
test01();
test02();
return 0;
}
输出:
---------------------------------------------------------------------------------
p1的年龄为:18
p2的年龄为:40
3. 空指针访问成员函数
C++中空指针也是可以调用成员函数的,但是要注意有没有用到this指针
如果用到this指针,需要加以判断,保证代码的健壮性
示例:
#include<iostream>
#include<string>
using namespace std;
//空指针调用成员函数
//人类
class Person
{
public:
void showClassName()
{
cout << "this is Person class" << endl;
}
void showPersonAge()
{
//报错原因:传入的指针为NULL
if (this == NULL)
{
cout << "this is NULL" << endl;
return;
}
//等价于:cout << "age = " << this->m_Age << endl;
cout << "age = " << m_Age << endl;
}
int m_Age;
};
void test01()
{
Person * p = NULL;
p->showClassName();
p->showPersonAge();
}
int main()
{
test01();
return 0;
}
输出:
---------------------------------------------------------------------------------
this is Person class
this is NULL
4. const修饰成员函数
常函数:
-
成员函数后加const后,我们称这个函数为常函数
-
常函数不可以修改成员属性
-
成员属性声明时加关键字mutable后,在常函数中依然可以修改
常对象:
-
声明对象前加const,则称该对象为常对象
-
常对象只能调用常函数
示例:
#include<iostream>
#include<string>
using namespace std;
//const修饰成员函数
//常函数
class Person
{
public:
//this指针的本质是 指针常量,指针的指向是不可以修改
//在本例中,实际上this指针定义为:const Person * const this;
//第一个const修饰符,是由于在成员函数showPerson()后面加了个const,意味着this指针指向的值不能修改
//第二个const修饰符,意味着this指针的指向不能修改,这是this指针默认的情况
void showPerson() const
{
//等价于this->m_A = 100;
// m_A = 100; //报错,常函数不可以修改成员属性,this指针指向的值也不能修改
// this == NLUU; //报错,不能修改this指针的指向
this->m_B = 100;
}
void func()
{
m_A = 100;
}
int m_A;
mutable int m_B; //特殊变量,即使在常函数中,也可以修改这个值
};
void test01()
{
Person p;
p.showPerson();
cout << "p.m_A = " << p.m_A << endl;
cout << "p.m_B = " << p.m_B << endl;
}
//常对象
void test02()
{
const Person p = Person(); //在对象前加const,变为常对象
// p1.m_A = 100;
p.m_B = 100;
//常对象只能调用常函数
p.showPerson();
// p.func(); //常对象,不可以调用普通成员函数,因为普通成员函数可以修改属性
cout << "p.m_A = " << p.m_A << endl;
cout << "p.m_B = " << p.m_B << endl;
}
int main()
{
test01();
test02();
return 0;
}
输出:
---------------------------------------------------------------------------------
p.m_A = 1403162824
p.m_B = 100
p.m_A = 0
p.m_B = 10