文章目录
一. 静态成员
① 静态成员变量
静态成员变量就是普通的成员变量前面加上static
关键字就是静态成员变量.
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/
#include <iostream>
using namespace std;
class Simple
{
public:
Simple()
{
mSimpleCountPrivate++;
cout << "Simple的构造函数被调用,已被构造的次数" << ++mSimpleCount << endl;
}
~Simple() {};
int get_obj_count(void)
{
return mSimpleCountPrivate;
}
public:
static int mSimpleCount;
private:
static int mSimpleCountPrivate; // 私有的静态成员变量,外部不能直接访问
};
int Simple::mSimpleCount = 0; // 类的静态成员变量需要在类外分配内存空间
int Simple::mSimpleCountPrivate = 0;
int main()
{
Simple s1; // 第一次构造
cout << "已经创建的对象的个数: " << Simple::mSimpleCount << endl;
Simple s2; // 第二次构造
cout << "已经创建的对象个数: " << s2.get_obj_count() << endl;
Simple s3; // 第三次构造
cout << "已经创建的额对象个数: " << s3.mSimpleCount << endl;
system("pause");
return 0;
}
结果:
② 静态成员函数
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/
#include <iostream>
using namespace std;
class Student
{
public:
Student(const char *name, int age, float score)
{
mName = name;
mAge = age;
mScore = score;
mTotal++;
mScores += mScore;
}
void show()
{
cout << mName << "的年龄是: " << mAge << ", 成绩是: " << mScore << endl;
}
static int get_total()
{
return mTotal;
}
static float get_scores()
{
return mScores;
}
private:
const char *mName;
int mAge = 0;
float mScore;
static int mTotal; // 总人数
static float mScores; // 总分数
};
// 定义静态成员变量
int Student::mTotal = 0;
float Student::mScores = 0.0;
int main()
{
(new Student("张三", 15, 100))->show();
(new Student("李四", 13, 89))->show();
(new Student("王五", 12, 100))->show();
(new Student("小明", 11, 82))->show();
int total = Student::get_total();
float scores = Student::get_scores();
cout << "当前共有" << total << "名学生,总成绩是" << scores << ", 平均分是: " << scores / total << endl;
system("pause");
return 0;
}
结果:
二. 成员变量和成员函数分开存储
① 空对象占用字节的大小
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/
#include <iostream>
using namespace std;
class Empty
{
// 空类什么都没有
};
int main()
{
// 实例化两个对象
Empty e1;
Empty e2;
cout << "e1 对象占用内存大小: " << sizeof(e1) << endl;
cout << "e2 对象占用内存大小: " << sizeof(e2) << endl;
cout << "e1 的地址: " << &e1 << endl;
cout << "e2 的地址: " << &e2 << endl;
system("pause");
return 0;
}
结果:
分析
② 成员函数和变量分开存储
三. this指针
① 为什么需要this指针
② this指针的用途
- 用途1: 当形参和成员变量同名时,可以用this指针来区分
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/
#include <iostream>
using namespace std;
class Person
{
public:
Person(string name,int age)
{
this->name = name;
this->age = age;
}
private:
string name;
int age = 0;
};
int main()
{
Person p("Fioman", 18);
system("pause");
return 0;
}
- 在类的非静态函数中返回对象本身,可使用return *this,用于链式编程.
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/
#include <iostream>
using namespace std;
class Person
{
public:
Person(int money)
{
this->money = money;
}
Person &add_money(Person &p)
{
this->money += p.money;
return *this;
}
public:
int money = 0;
};
int main()
{
Person p1(5);
Person p2(100);
Person p3(20);
p1.add_money(p2); // 5 + 100 = 105
p1.add_money(p2).add_money(p3); // 105 + 100 + 20 = 225
cout << p1.money << endl;
system("pause");
return 0;
}
③ this指针使用的时候注意事项
- this指针只能在成员函数中使用
class A
{
public:
// 这个函数的原型应该是int func(A* const this,int p){}
int func(int p)
{
cout << "in func this 指针的地址: " << this << endl;
}
};
- this在成员函数开始前构造,在成员函数结束后清除
- this指针是什么时候创建的
- this指针存放在何处?堆,栈,全局变量,还是其他?
- this指针是如何传递类中的函数的
- this指针是如何访问结构体中的变量的
- 我们只有获得一个对象后,才能通过对象this指针,如果知道了一个this指针的位置,可以直接使用吗?
- 每个类编译后,是否创建一个类中函数表保存函数指针,以便用来调用函数?