C++Day4
静态成员
-
在类的定义中,其成员(包括成员变量与成员函数)都可以使用关键字static声明为静态,称为静态成员
-
不管这个类创建了多少个对象,静态成员只有一个拷贝,这个拷贝被所有属于这个类的对象共享
-
#include <iostream> #include <string> using namespace std; class Person { public: //静态成员变量:编译阶段就分配了内存 //类内声明、类外初始化 //静态成员变量 所有对象都共享一份数据 static int m_A; //静态成员函数 //所有对象都共享同一个func函数 static void func() { //m_C=100;静态成员函数不能访问非静态成员变量,无法区分到达要修改哪个对象 m_A = 100; cout << "func调用" << endl; } int m_C; private: static int m_B; static void func2() { } }; //int m_A=0错误 为全局变量,应加作用域 int Person::m_A = 0; void test01() { //通过对象进行访问 Person p1; cout << p1.m_A << endl;//0 Person p2; p2.m_A = 100; cout << p1.m_A << endl;//100 数据共享 //通过类名进行访问 cout << Person::m_A << endl; //静态成员变量也有访问权限,私有权限类外访问不到 //cout<<Person::m_B<<endl; } void test02() { //通过对象 Person p1; p1.func(); //通过类名 Person::func(); //Person::func2();静态成员函数也有访问权限 } int main() { test01(); test02(); return EXIT_SUCCESS; }
单例模式
-
在单例模式的核心结构中只包含一个被称为单例的特殊类。通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源
-
//单例模式:主席类案例 #include <iostream> #include <string> using namespace std; class ChairMan { public: static ChairMan* getInstance() { return singleMan; } private: //将构造函数私有化,不可以创建多个对象 ChairMan() {}; ChairMan(const ChairMan&) {}; //public: private: //将主席指针私有化,对外提供只读接口 static ChairMan* singleMan;//类内声明,类外初始化 }; ChairMan* ChairMan::singleMan = new ChairMan; void test01() { ChairMan*c1=ChairMan::getInstance(); //ChairMan* c2= ChairMan::getInstance(); //c1与c2相同 //ChairMan * c3= new ChairMan(*c1); //c1与c3不相同 等于克隆出一个主席 可增加一拷贝构造函数私有化将此屏蔽掉 } int main() { test01(); return EXIT_SUCCESS; }
C++对象模型初探
-
#include <iostream> #include <string> using namespace std; class Person { }; void test01() { //空类的sizeof是1,因为每个对象都应该在内存上有独一无二的地址,因此给空对象分配一个字节空间 Person p1; Person pArr[10]; cout << "sizeof=" << sizeof(p1) << endl;//1 cout << "sizeof=" << sizeof(pArr) << endl;//10 } int main() { test01(); return EXIT_SUCCESS; }
-
#include <iostream> #include <string> using namespace std; class Person { public: int m_A; void func(){} }; void test01() { //C++实现了封装,数据(成员属性)和操作(成员函数)是分开存储的 //C++的非静态数据成员直接内含在类对象中 //成员函数在class声明,却不在对象中 //静态成员变量与静态成员函数不属于类对象上 Person p1; cout << "sizeof=" << sizeof(p1) << endl;//4 } int main() { test01(); return EXIT_SUCCESS; }
this指针
-
class Person { public: Person(int age) { age = age;//无法区分导致下面出错 } int age; }; void test01() { Person p1(18); cout << "p1年龄" << p1.age << endl;//会输出乱码 }
-
class Person { public: Person(int age) { //用途一:解决名称冲突 //p1上的age this->age = age; } //this指针隐式加在每个成员函数中 bool compareAge(Person &p)//bool compareAge(Person *this,Person &p) { if(this->age==p.age) { return true; } return false; } int age; }; void test01() { //this指针指向被调用的成员函数所属的对象 Person p1(18); cout << "p1年龄" << p1.age << endl; }
空指针访问成员函数
#include <iostream>
using namespace std;
class Person
{
public:
void showClass()
{
cout << "class Name is Person" << endl;
}
void showAge()
{
if (this == NULL)
{
return;
}//此段若注释掉会出现错误
m_Age = 0;
cout << "age=" << this->m_Age << endl;
}
int m_Age;
};
void test01()
{
Person* p = NULL;
p->showClass();
p->showAge();
}
int main()
{
test01();
return EXIT_SUCCESS;
}
//如果成员函数中没有用到指针,可以用空指针调用成员函数
//如果成员函数中用到了this,那么此this需要加判断,防止代码down掉
常函数和常对象
#include <iostream>
using namespace std;
class Person
{
public:
Person(int age)
{
this->m_Age = age;
}
//常函数:在函数形参后加上const
//修饰成员函数中的this指针,让指针指向的值不可以修改
void showPerson()const
{
//this指针的本质:Person * const this
//this=NULL;错误,指针的指向不可以修改,但指针指向的值可以修改
cout << "person age=" << m_Age << endl;
}
void func()
{
}
int m_Age;
mutable int m_A;
//常函数或常对象中有些特殊的出现依然想修改,可在定义时加关键字mutable
};
void test01()
{
//常对象 在定义前加上const
const Person p1(10);
//p1.m_Age=10;不可修改
p1.m_A = 10;//可修改
//常对象可以调用常函数
p1.showPerson();
//p1.func();常函数不可以调用普通函数
}
int main()
{
return EXIT_SUCCESS;
}
全局函数做友元函数
-
可以将全局函数、某个类中的成员函数、整个类声明为友元
-
#include <iostream> using namespace std; #include <string> class Building { friend void goodGay(Building* building); //利用friend关键字让全局函数goodGay作为本类好朋友可以访问私有成员 public: Building() { this->m_SittingRoom = "客厅"; this->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 EXIT_SUCCESS; }
类作为友元类
#include <iostream>
using namespace std;
#include <string>
class Building;
class GoodGay
{
public:
GoodGay();
void visit();
Building* m_building;
};
class Building
{
//让goodGay类作为Building的好朋友可以访问私有成员
friend class GoodGay;
public:
Building();
string m_SittingRoom;
private:
string m_BedRoom;
};
//类外初始化
Building::Building()
{
this->m_SittingRoom = "客厅";
this->m_BedRoom = "卧室";
}
GoodGay::GoodGay()
{
this->m_building=new Building;
}
void GoodGay::visit()
{
cout << "好基友正在访问" << this->m_building->m_SittingRoom << endl;
cout << "好基友正在访问" << this->m_building->m_BedRoom << endl;
}
void test01()
{
GoodGay gg;
gg.visit();
}
int main()
{
test01();
return EXIT_SUCCESS;
}
类中的成员函数作为友元函数
#include <iostream>
using namespace std;
#include <string>
class Building;
class GoodGay
{
public:
GoodGay();
void visit();//可以访问building的私有
void visit2();//不可以访问building的私有
Building* m_building;
};
class Building
{
//让GoodGAy类中的visit成员函数作为友元
friend void GoodGay::visit();
public:
Building();
string m_SittingRoom;
private:
string m_BedRoom;
};
//类外初始化
Building::Building()
{
this->m_SittingRoom = "客厅";
this->m_BedRoom = "卧室";
}
GoodGay::GoodGay()
{
this->m_building = new Building;
}
void GoodGay::visit()
{
cout << "好基友正在访问" << this->m_building->m_SittingRoom << endl;
cout << "好基友正在访问" << this->m_building->m_BedRoom << endl;
}
void test01()
{
GoodGay gg;
gg.visit();
}
int main()
{
test01();
return EXIT_SUCCESS;
}