在C++中,类内的成员变量和成员函数分开存储
只有非静态成员变量才属于类的对象上
#include<iostream>
#include<string>
using namespace std;
//成员变量和成员函数是分开存储的
class Person{
int m_A; //非静态成员变量,属于类的对象上
static int m_B; //静态成员变量,不属于类的对象上
void func(){} //非静态成员函数,不属于类的对象上
static void func(){} //静态成员函数,不属于类的对象上
};
int Person::m_B = 10;
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(){
test02();
return 0;
}
?
通过1我们知道了C++中成员变量和成员函数是分开存储的
每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码
那么问题是:这一块代码是如何区分哪个对象调用的自己呢?
?
C++通过提供特殊的对象指针,this指针,解决上述问题。this指针指向被调用的成员函数所属的对象
?
this指针是隐含每一个非静态成员函数的一种指针
this指针不需要定义,直接使用即可
?
this指针的用途:
#include<iostream>
using namespace std;
class Person{
public:
Person(int age){
//this指针指向被调用的成员函数所属的对象
this->age = age;
}
int age;
Person& PersonAddAge(Person &p){
this->age += p.age;
return *this;
}
};
//1. 解决名称冲突
void test01(){
Person p1(18);
cout << "p1的年龄为:" << p1.age << endl;
}
//2. 返回对象本身用*this
void test02(){
Person p1(18);
Person p2(18);
//链式编程思想
p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
cout << "p2的年龄为:" << p2.age << endl;
}
int main(){
test02();
return 0;
}
?
C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针
如果用到this指针,需要加以判断保证代码的健壮性
#include<iostream>
using namespace std;
class Person{
public:
void showClassName(){
cout << "this is Person class" << endl;
}
void showPersonAge(){
//错误原因是因为传入的指针是为NULL
if(this == NULL){
return ;
}//预防错误
cout << "age = " << m_age /*this->m_age*/ << endl;
}
int m_age;
};
void test01(){
Person * p = NULL;
p->showClassName();
p->showPersonAge();
}
int main(){
test01();
return 0;
}
?
常函数:
常对象:
在成员函数后加const,修饰的是this指向,让指针指向的值也不可以修改
#include<iostream>
using namespace std;
class Person{
public:
//this指针的本质是指针常量,指针的指向是不可以修改的
//const Person * const this;
//在成员函数后加const,修饰的是this指向,让指针指向的值也不可以修改
void showPerson() const {
m_b = 100;
m_a = 100;
cout << "this is Person class" << endl;
}
void func(){
}
int m_a;
mutable int m_b; //特殊变量,即使在常函数中,也可以修改这个值,加关键字mutable
};
void test01(){
Person * p = NULL;
p->showClassName();
p->showPersonAge();
}
void test02(){
const Person p;
p.m_a = 100; //不可以修改
p.m_b = 100; //可以修改
p.showPerson(); //可以调用,是因为两个都不可以修改普通成员变量
p.func(); //不可以调用
}
int main(){
test01();
return 0;
}
原文:https://www.cnblogs.com/maeryouyou/p/12017904.html