一、C++对象模型和this指针
1.成员变量和成员函数分开储存
#include<iostream>
using namespace std;
class Person
{
int m_A;
static int m_B;
void func() {}
static void func2(){}
};
int Person::m_B = 0;
void test01()
{
Person p;
cout << "size of p=" << sizeof(p) << endl;
}
void test02()
{
Person p;
cout << "size of p=" << sizeof(p) << endl;
}
int main()
{
test02();
return 0;
}
2.this指针概念
#include<iostream>
using namespace std;
class Person
{
public:
Person(int age)
{
this->age = age;
}
Person& PersonAddAge(Person &p)
{
this->age += p.age;
return *this;
}
int age;
};
void test01()
{
Person p1(18);
cout << "p1的年龄为" << p1.age << endl;
}
2.返回对象本身用*this
void test02()
{
Person p1(10);
Person p2(10);
p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
cout << "p2的年龄为" << p2.age << endl;
}
int main()
{
test02();
return 0;
}
3.空指针访问成员函数
#include<iostream>
using namespace std;
class Person
{
public:
void showClassName()
{
cout << "this is Person class" << endl;
}
void showPersonAge()
{
if (this == NULL)
{
return;
}
cout << "age =" << this->m_Age << endl;
}
int m_Age;
};
void test01()
{
Person* p = NULL;
p->showPersonAge();
}
int main()
{
test01();
return 0;
}
4.const修饰成员函数
#include <iostream>
using namespace std;
class Person
{
public:
void showPerson() const
{
this->m_B = 100;
}
void func()
{
}
int m_A;
mutable int m_B;
};
void test01()
{
Person p;
p.showPerson();
}
void test02()
{
const Person p;
p.m_B = 100;
p.showPerson();
}
int main()
{
test01();
return 0;
}
二、友元
1.全局函数做友元
#include <iostream>
using namespace std;
#include <string>
class Building
{
friend void goodGay(Building* building);
public:
Building()
{
m_SittingRoom = "客厅";
m_BedRoom = "卧室";
}
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
void goodGay(Building* building)
{
cout << "好基友全局函数 正在访问:" << building->m_SittingRoom << endl;
cout << "好基友全局函数 正在访问:" << building->m_BedRoom << endl;
}
void test01()
{
Building building;
goodGay(&building);
}
int main()
{
test01();
return 0;
}
2.类做友元
#include <iostream>
using namespace std;
#include <string>
class Building;
class GoodGay
{
public:
GoodGay();
void visit();
Building* building;
};
class Building
{
friend class GoodGay;
public:
Building();
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
Building::Building()
{
m_SittingRoom = "客厅";
m_BedRoom = "卧室";
}
GoodGay::GoodGay()
{
building = new Building;
}
void GoodGay::visit()
{
cout << "好基友类 正在访问" << building->m_SittingRoom << endl;
cout << "好基友类 正在访问" << building->m_BedRoom << endl;
}
void test01()
{
GoodGay gg;
gg.visit();
}
int main()
{
test01();
return 0;
}
3.成员函数做友元
#include <iostream>
using namespace std;
class Building;
class GoodGay
{
public:
GoodGay();
void visit();
void visit2();
Building* building;
};
class Building
{
friend void GoodGay::visit();
public:
Building();
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
Building::Building()
{
m_SittingRoom = "客厅";
m_BedRoom = "卧室";
}
GoodGay::GoodGay()
{
building = new Building;
}
void GoodGay::visit()
{
cout << "visit函数正在访问" << building->m_SittingRoom << endl;
cout << "visit2函数正在访问" << building->m_BedRoom << endl;
}
void GoodGay::visit2()
{
cout << "visit函数正在访问" << building->m_SittingRoom << endl;
}
void test01()
{
GoodGay gg;
gg.visit();
gg.visit2();
}
int main()
{
test01();
return 0;
}
|