目录
P113类和对象-静态成员
静态成员就是在成员变量和成员函数前加上static,称为静态成员。
静态成员分为:
- 静态成员变量:
- 所有对象共享同一份数据
- 在编译阶段分配内存
- 类内声明,类外初始化
- 静态成员函数
- 所有对象共享同一个函数
- 静态成员函数只能访问静态成员变量
静态成员变量示例:
using namespace std;
#include<iostream>
#include<string>
//静态成员变量
class Person {
public:
//所有对象都共享同一份数据
//编译阶段就分配内存
//类内声明,类外初始化操作
static int m_A;
//静态成员变量也是有访问权限的
private:
static int m_B;
};
int Person::m_A = 100;
int Person::m_B = 200;
void test01() {
Person p;
cout << p.m_A << endl;
Person p2;
p2.m_A = 200;
cout << p.m_A << endl;
}
void test02() {
//静态成员变量 不属于某一个对象,所有对象共享同一份数据
//因此静态成员变量有两种访问方式
//1、通过对象进行访问
Person p;
cout << p.m_A << endl;
//2、通过类名进行访问
cout << Person::m_A << endl;//Person::m_A可读为Person作用域下的m_A
//cout << Person::m_B << endl; 类外访问不到私有静态成员变量
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
静态成员函数示例:
using namespace std;
#include<iostream>
#include<string>
//静态成员函数
//程序共享同一个函数
//静态成员函数只能访问静态成员变量
class Person {
public:
//静态成员函数
static void func() {
m_A = 100;//静态成员函数可以访问静态成员变量
//m_B = 200;//静态成员函数不可以访问非静态成员变量,无法区分到底是哪个对象的m_B;
cout << "static void func调用" << endl;
}
//静态成员函数也是有访问权限的
private:
static void func2() {
cout << "static void fun2()调用" << endl;
}
static int m_A;
int m_B;
};
int Person::m_A = 0;
//有两种访问方式
void test01() {
//1、通过对象进行访问
Person p;
p.func();
//2、通过类名进行访问
Person::func();
//Person::func2();//类外访问不到私有的静态成员函数
}
int main()
{
test01();
system("pause");
return 0;
}
P114类和对象-对象特性-成员变量和成员函数分开存储
在C++中,类内的成员变量和成员函数分开存储,只有非静态成员变量才属于类的对象上。
using namespace std;
#include<iostream>
#include<string>
//成员变量和成员函数分开存储
class Person {
int m_A;//非静态成员变量 属于类的对象上的
static int m_B;//静态成员变量 不属于类的对象上的
void func() { };//非静态成员函数 不属于类的对象上
static void func2() { };//静态成员函数 不属于类的对象上
};
int Person::m_B=0;
void test01() {
Person p;
//空对象(Person类中为空)占用的内存空间为:1
//C++编译器会给每个空对象也分配一个字节空间,是为了区分空对象占内存的位置
//每个空对象也应该有一个独一无二的内存地址
cout << " size of p:" << sizeof(p) << endl;
}
void test02() {
Person p;
cout << " size of p:" << sizeof(p) << endl;
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
结果:
当Person类中为空时,sizeof(p)为1;
当Person类中有int m_A; static int m_B;时,sizeof(p)为4;
当Person类中有int m_A; static int m_B;void func() { };时,sizeof(p)为4;
当Person类中有int m_A; static int m_B;void func() { };static void func2() { };时,sizeof(p)为4;
---->说明:只有非静态成员变量在类的对象上。
P115类和对象-对象特性-this指针的用途
在C++中成员变量和成员函数是分开存储的。每一个非静态成员函数只会诞生一份函数实例,也就是多个同类型的对象共用一块代码。那么问题是:这一块代码是如何区分哪个对象调用自己呢?
C++通过提供特殊的对象指针,this指针,解决上述问题。this指针指向被调用的成员函数所属的对象。this指针是隐含每一个非静态成员函数内的一种指针;this指针不需要定义,直接使用即可。
this指针的用途:
在形参和成员变量同名时,可以用this指针来区分;
在类的非静态成员函数中返回对象本身,可使用return *this。
using namespace std;
#include<iostream>
#include<string>
//1 解决名称冲突问题
//2 返回对象本身用this
class Person {
public:
Person(int age) {
this->age = age;
}
Person& PersonAddage(Person& p) {
this->age += p.age;
//this指向p2的指针,而*this指向的就是p2这个对象本体
return *this;
}
int age;
};
void test01() {
Person p(18);
cout <<"p的年龄为:" << p.age << endl;
}
//2 返回对象本身用this
void test02() {
Person p1(10);
Person p2(10);
//链式编程思想
p2.PersonAddage(p1).PersonAddage(p1);
cout << "p2的年龄为:" << p2.age << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
运行后的结果为:
P116类和对象-对象特性-空指针访问成员函数
C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针。
如果用到this指针,需要加以判断保证代码的健壮性
示例:
using namespace std;
#include<iostream>
//空指针调用成员函数
class Person {
public:
void showClassName()
{
cout << "this is Person class" << endl;
}
void showAge() {
cout << "age = " << m_Age << endl;
}
int m_Age;
};
void test01() {
Person* p = NULL;
p->showClassName();
p->showAge();
}
int main()
{
test01();
system("pause");
return 0;
}
运行后的结果为:
视频课程中运行到showAge()时会异常, 因此将showAge函数修改为:
void showAge() {
//报错原因是因为传入的指针为NULL;
if (this == NULL) {
return;
}
cout << "age = " << m_Age << endl;
}
P117类和对象-对象特性-const修饰成员函数
常函数:
- 成员函数后加const后我们称这个函数为常函数
- 常函数内不可以修改成员属性
- 成员属性声明时加关键字mutable后,在常函数中依然可以修改
常对象:
- 声明对象前加const称该对象为常对象
- 常对象只能调用常函数
示例:
using namespace std;
#include<iostream>
//常函数
class Person {
public:
//this指针的本质 是指针常量 指针的指向不可以修改
//const Person *const this
//在成员函数后加const,修饰的是this指向,让指针指向的值也不可以修改
void showPerson() const
{
this->m_B = 100;
//this->m_A = 100;
//this = NULL; //this指针不可以修改指针的指向
}
int m_A;
mutable int m_B;//特殊变量,即使在常函数中,也可以修改这个值,加关键字mutable
};
//常对象
void test02() {
const Person p;//在对象前加const,变为常对象
//p.m_A = 100;
p.m_B = 10;//m_B是特殊值,在常对象下也可以修改
//常对象只能调用常函数
p.showPerson();
}
void test01() {
}
int main()
{
test01();
system("pause");
return 0;
}