5,411
社区成员




3.1.1.1空对象的大小 - 面试真题
class Person {
public:
};
void test(){
Person p;
cout << "sizeof(p) == " << sizeof(p) << endl;
}
3.1.1.2类对象的大小关系
class Person {
public:
int m_A;//非静态成员变量
};
void test(){
Person p;
cout << "sizeof(p) == " << sizeof(p) << endl;
}
class Person {
public:
int m_A;//非静态成员变量
static int m_B;
};
int Person::m_B = 0;//类内声明,类外初始化
void test(){
Person p;
cout << "sizeof(p) == " << sizeof(p) << endl;
}
class Person {
public:
int m_A;//非静态成员变量
static int m_B;
void func(){};
};
int Person::m_B = 0;//类内声明,类外初始化
void test(){
Person p;
cout << "sizeof(p) == " << sizeof(p) << endl;
}
class Person {
public:
int m_A;//非静态成员变量
static int m_B;//静态成员变量
void func(){};//非静态成员函数
static void func2(){};//静态成员函数
};
int Person::m_B = 0;//类内声明,类外初始化
void test(){
Person p;
cout << "sizeof(p) == " << sizeof(p) << endl;
}
//假设在类中存在一个函数(成员函数)
void func(){ }
//其中在类外有很多实例化的对象,如p1,p2,p3...,都在调用函数 func() (其中func()只有一份),那么在函数体中如何区分是p1在调用 func(),还是p2在调用func()呢?...该怎么区分是要修改p1的属性?还是修改p2的属性?...
//在这里,我们就使用到了thishi指针的概念了
//this指针指向被调用的成员函数的所属对象,比如p1调用成员函数,那么this->p1;
return* this
3.2.1.1解决名称冲突
p1(18)^(调用)^ --->Person( int age )^(有参构造函数)^
,相当于 this指针^指向^ --->p1(被调用的成员函数 所属 的对象)
3.2.1.2返回对象本身 — *this
#include<iostream>
using namespace std;
class Person {
public:
Person(int age) {
this->age = age;
}
//返回值为 void
void PersonAddAge(Person& p) {
this->age += p.age;
^ ^
(this指针指向)p2 p1
}
int age;
};
void test() {
Person p1(18);
Person p2(20);
p2.PersonAddAge(p1);//为 p2 属性增值
cout << "p2的年龄为: " << p2.age << endl;
//当我们持续为 p2 增值是无法实现的
p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);--->false
}
int main(){ test();return 0; }
3.2.1.3链式编程思想 - 链式调用
class Person {
public:
Person(int age) {
this->age = age;
}
//Person& 表示返回一个Person的引用,使用返回this指针返回 成员对象age的本体需要使用引用的方式
Person& PersonAddAge(Person& p) {
this->age += p.age;//将参数对象的属性加到当前对象的属性上
return* this; //返回当前对象的引用,支持链式调用
//因为this 是一个指向p2的指针,所以*this 指向的就是这个对象的本体
}
int age;
};
void test() {
Person p1(18);
Person p2(20);
//链式编程思想 - 链式调用
p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
cout << "p2的年龄为: " << p2.age << endl;
}
int main(){test(); return 0;}
3.2.1.4引用置于链式调用的关键地位
返回 Person 的引用
Person& PersonAddAge(Person& p)
返回 Person 的值
更改为 ====>> Person PersonAddAge(Person& p)
输出结果???
//使用空指针调用成员函数
class Person {
public:
void showClassName() {
cout << "This is Person Class!" << endl;
}
void showPersonAge()
{
cout << "This Person age is: " << age << endl;
}
int age;
};
void test() {
Person* p = NULL;//指针数据类型是Person*,指向空
p->showClassName();--->true
p->showPersonAge();--->false
}
代码错误分析:
(this->)age,告诉用户这是当前对象的属性(成员变量)
void showPersonAge(){
在我们类中函数的属性,其实都默认自带 (this->)age,告诉用户这是当前对象的属性(成员变量)
cout << "This Person age is: " << age << endl;
}
但问题就出在:Person* p = NULL;
在this指针的概念中,我们有详细说明this指针的指向实质是:被调用的成员函数 所属的对象
因此this->p(p == NULL),导致this->NULL,而并未指向一个确切的数据;
所以this 所指向的并不是一个实体(因为p为NULL,因此实例对象p,可视为不存在,因此Person并没有实例出任何对象,所以更不可能通过一个空值(this->p)调用类中的成员变量!)
class Person {
public:
void showClassName() {
cout << "This is Person Class!" << endl;
}
void showPersonAge()
{
//设置判断条件,保证代码的健壮性
if(this == NULL)
{
return;
}
cout << "This Person age is: " << age << endl;
}
int age;
};
void test() {
Person* p = NULL;//指针数据类型是Person*,指向空
p->showClassName();--->true
p->showPersonAge();--->false
}
class Person{
public:
void showPerson()
const{ (this->)a = 100;//--->false
(this->)b = 200;//--->true
}//--->const Person* const this;
int a;
mutable int b;//使 b 变为特殊值,在常函数下可修改
}
void test(){
const Person p;//在对象前加const,变为常对象
p.a = 20;//--->false
p.b = 30;//--->true,b 为特殊值,在常对象下可修改
//常对象只能调用常函数
p.showPerson();//--->true
p.func(普通成员函数);//--->false,
}
文章来源: https://blog.csdn.net/2401_87692970/article/details/146245275
版权声明: 本文为博主原创文章,遵循CC 4.0 BY-SA 知识共享协议,转载请附上原文出处链接和本声明。