成员变量和成员函数分开存储
空对象占用内存空间为 1
C++编译器会给每个空对象分配一个字节空间,是为了区分空对象占内存的位置。
每个空对象也应该有一个独一无二的内存地址。
只有非静态的成员变量在对象中占据空间,其他的成员函数和静态成员变量都是共有的。
静态或者非静态的成员函数,都是公用的,每个创建的对象都会访问(使用)同一个成员函数。
那么如何区分到底是哪个对象调用函数呢?
使用this指针,this指针指向被调用的成员函数所属的对象。
#include <iostream>
using namespace std;
class Person {
public:
// 非静态成员变量占对象空间 属于类的对象 4字节
int mA;
// 静态成员变量不占对象空间 不属于类的对象
static int mB;
Person() {
mA = 0;
}
// 函数也不占对象空间,所有函数共享一个函数实例
void func() {
cout << "func()" << endl;
}
// 静态成员函数也不占对象空间
static void func2(){}
};
// 空对象
class A {
};
void test0() {
Person p;
cout << sizeof(p) << endl;
}
void test1() {
A a;
cout << sizeof(a) << endl;
}
int main() {
test0();
test1();
return 0;
}
this指针,使用this指针,this指针指向被调用的成员函数所属的对象。
1.this指针隐含在每一个非静态成员函数内。
2.this指针不需要定义,直接使用即可。
this的作用:
1.解决名称冲突
2.返回对象本身用*this
有两种返回方式,一种是Person PersonAddAge(Person &p),另一种Person& PersonAddAge(Person &p)
第一种是返回值,第二种是返回引用。
返回值的方式相当于调用拷贝构造函数,拷贝到一个副本,临时的副本,返回的不是原来的对象p,第二个是返回的原来的对象p
#include <iostream>
using namespace std;
class Person {
public:
int age;
// 1.解决名称冲突
Person(int age)
{
// 当形参和成员变量同名时,可使用this指针区分
// this指针指向被调用的成员函数所属的对象(本例test1中的p1),等价于p1.age = age;
this -> age = age;
}
// 2.返回对象本身用*this
void PersonAddAge(Person& p) {
this -> age += p.age;
}
// 返回p2本身,使用&引用方式 返回指针需要用& 引用的方式
Person& PersonAddPerson(Person& p) {
this -> age += p.age;
// this是指向p2的指针,*this指向的就是就是p2本身
return *this;
}
// 区别:返回引用(即对象本身)和返回值(即Person类型)不同:
Person PersonAddPerson2(Person& p) {
this -> age += p.age;
return *this;
}
// 如果下例中的p4调用的是PersonAddPerson2,则最终的结果为20,因为
};
void test1() {
Person p1(18);
cout << "p1的年龄为:" << p1.age << endl;
}
void test2() {
Person p2(20);
Person p3(20);
p2.PersonAddAge(p3);
cout << "p2的年龄为:" << p2.age << endl;
p3.PersonAddAge(p2);
cout << "p3的年龄为:" << p3.age << endl;
}
void test3() {
Person p4(20);
Person p5(20);
// 链式编程思想1
p4.PersonAddPerson(p5).PersonAddAge(p4); // 这句话如果输出的话是40 ,但是p4没有被修改
cout << "p4的年龄为:" << p4.age << endl; // 所以直接返回p4 原本的值
}
int main()
{
test1();
test2();
/*
* Person p4(20);
* p4.PersonAddAge(p3).PersonAddAge(p2);
* 当前来看是不合法操作,原因是PersonAddAge返回的是void,不能进行链式调用
* 解决方法:将函数的返回值类型改为Person&类,同时返回值改为return *this;(返回值指向p2本身,且类型为Person&)
*/
test3();
return 0;
}
空指针访问成员函数
C++中空指针可以调用成员函数,但是得注意用没用this指针
#include <iostream>
using namespace std;
class Person {
public:
int mAge;
void ShowClassName() {
cout << "我是Person类!" << endl;
}
void showPerson() {
if (this == NULL) { // 如果没有该限制条件,this指针为空调用函数会报错
return;
}
cout << "年龄为:" << mAge << endl; // 这里默认 this->mAge 调用成员函数this指向p,p是空指针,所以会报错。而不是一个确切的对象。
}
};
void test01() {
Person *p = NULL;
p -> ShowClassName(); // 空指针,但是可以调用成员函数
p -> showPerson(); // 但是如果成员函数中用到了this指针就不可以
}
int main() {
test01();
return 0;
}
const修饰成员函数
成员函数后面加const我们称这个函数为常函数。
常函数内不可以修改成员属性。
成员属性声明时加关键字mutable后,在常函数中依然可以修改。
常对象
声明对象前加const称该对象为常对象。
常对象只能调用常函数。
常函数
this本质是指针常量,指针的指向不可以修改
void showPerson() 相当于 Person * const this;相当于指向不可以修改的指针常量。
想进一步限制指针指向的值不可以修改需要再加个const
const Person * const this
也就是void showPerson() const
在成员函数后面加一个const,修饰的是this指向,让指针指向的值也不可以修改
#include <iostream>
using namespace std;
// 1.常函数
class Person {
public:
int m_A;
mutable int m_B; // 可修改,可变的
Person() {
m_A = 0;
m_B = 0;
}
// this指针本质是一个指针常量(Person *const this),指针的指向不可修改
// 如果想让指针指向的值也不可以修改,需要声明常函数
void showPerson() const { //成员函数后面加const,修饰的是this指针,使指针指向的值不可修改
// this -> m_A = 100; //不可修改,因为this指针指向的值是const修饰的
this -> m_B = 100; //可修改,因为m_B变量是mutable修饰的
}
void func() {
cout << "非常函数" << endl;
}
};
// 2.常对象
void test01() {
const Person p; // 在对象前面 加const修饰,修饰的是对象,对象不可修改
cout << p.m_A << endl; // 正确写法,常对象可以访问成员变量的值
// p.m_A = 100; // 错误写法,常对象不能修改成员变量的值,但是可以访问
p.m_B = 100; // 可以修改mutable修饰的 成员变量
// 常对象只能调用常函数
p.showPerson(); // 正确
// p.func(); // 错误, 常对象不能调用普通成员函数,因为普通成员函数可以修改属性
}
int main() {
test01();
return 0;
}