C++类和对象-C++对象模型和this指针->成员变量和成员函数分开存储、this指针概念、空指针访问成员函数、const修饰成员函数

#include<iostream>

using namespace std;

//成员变量 和 成员函数 分开储存的

class Person {

public:

Person() {

mA = 0;

}

//非静态成员变量占对象空间

int mA;

//静态成员变量不占对象空间

static int mB;

//函数也不占对象空间,所有函数共享一个函数实例

void func() {

cout << "mA:" << this->mA << endl;

}

//静态成员函数也不占对象空间

static void sfunc() {

}

};

int Person::mB = 0;

void test01()

{

Person p;

//空对象占用内存空间: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;

}

#include<iostream>

using namespace std;

class Person

{

public:

Person(int age)

{

//1、当形参和成员变量同名时,可用this指针来区分

//this指针指向 被调用的成员函数 所属的对象

this->age = age;

}

Person& PersonAddPerson(Person p)

{

this->age += p.age;

//返回对象本身

//this指向p2的指针,而*this指向的就是p2这个对象的本体

return *this;

}

int age;

};

void test01()

{

//1.解决名称冲突

Person p1(10);

cout << "p1.age = " << p1.age << endl;

//2.返回对象本身用*this

Person p2(10);

//链式编程思想

p2.PersonAddPerson(p1).PersonAddPerson(p1).PersonAddPerson(p1);

cout << "p2.age = " << p2.age << endl;

}

int main() {

test01();

system("pause");

return 0;

}

#include<iostream>

using namespace std;

//空指针调用成员函数

class Person

{

public:

void ShowClassName()

{

cout << "我是Person类!" << endl;

}

void ShowPersonAge()

{

if (this == NULL)

{

return;

}

//报错原因是传入的指针为NULL

cout << this->mAge << endl;

}

public:

int mAge;

};

void test01()

{

Person * p = NULL;

p->ShowClassName(); //空指针,可以调用成员函数

p->ShowPersonAge(); //但是如果成员函数中用到了this指针,就不可以了

}

int main() {

test01();

system("pause");

return 0;

}

#include<iostream>

using namespace std;

//常函数

class Person

{

public:

Person()

{

m_A = 0;

m_B = 0;

}

//this指针的本质是一个指针常量,指针的指向不可修改

//const Person * const this

//在成员函数后面加const,修饰的是this指向,让指针指向的值也不可以修改

void ShowPerson() const

{

//this = NULL; //this指针不可以修改指针的指向 Person* const this;

//this->mA = 100; //但是this指针指向的对象的数据是可以修改的

//const修饰成员函数,表示指针指向的内存空间的数据不能修改,除了mutable修饰的变量

this->m_B = 100;

}

void MyFunc()

{

mA = 10000;

}

public:

int m_A;

mutable int m_B; //特殊变量,即使在常函数中,也可以修改这个值

};

//const修饰对象 常对象

void test01() {

const Person person; //在对象前加const,变为常对象

cout << person.m_A << endl;

//person.mA = 100; //常对象不能修改成员变量的值,但是可以访问

person.m_B = 100; //但是常对象可以修改mutable修饰成员变量

//常对象只能调用常函数

person.ShowPerson();

//person.MyFunc(); //常对象 不能调用普通成员函数,因为普通成员函数可以修改属性

}

int main() {

test01();

system("pause");

return 0;

}

相关推荐
李纲明1 分钟前
WordPress 媒体库上传失败或一直转圈:先核 PHP 上传限制与目录权限,再查安全插件与对象存储
运维·开发语言·安全·php·wordpress·媒体库·devdiy
第九序言T8 小时前
Python 数据处理进阶(十八)批量处理同结构文件:一个文件夹一次跑完
开发语言·python·自动化·pandas·数据处理
Sakura_j_h_9 小时前
EasyEDA PCB 快速设计Skills
c++·python·github
龙亘川10 小时前
构建城市公共安全数字底座:亘川智城智慧安防系统全维度解读
java·大数据·开发语言·人工智能·信息可视化
程序喵大人10 小时前
【C++入门】内存模型与资源管理 - 09 shared_ptr 和 weak_ptr 解决共享,也带来代价
开发语言·c++·资源管理·智能指针·内存模型
Raas10010 小时前
AI网关部署方案?从部署到上线全流程
java·开发语言·人工智能·gateway·ai网关·企业级ai网关·企业级ai产品
专注仿真11 小时前
线性回归和逻辑回归算法详解
开发语言·人工智能·python·大模型训练·控制算法
jimy112 小时前
C++ 中的 `T&&`:右值引用与转发引用
开发语言·c++
呆呆虫1212 小时前
用 orjson 加速 SerpBase 响应解析:基准对比与注意事项
开发语言·php
心疼你的一切13 小时前
Python自动发邮件实战定时发报表
大数据·开发语言·python