马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
01 特殊的数据成员 (是我写作业不看标题
1,常量数据成员
加上const 的数据成员,加上之后只能在声明和初始化队列中举行初始化,初始化之后不能举行赋值利用
2,引用数据数据成员
用引用方式创建的数据成员
引用数据成员须要在初始化列表中举行初始化
须要绑定一个已经存在的,在引用成员的生命周期内始终有效的变量(对象
3,对象成员
一个类的对象是其他类的数据成员
coution:
初始化列表写对象,不写类名,不能在声明对象成员时直接用有参构造创建
假如在初始化列表中没有表现的初始化对象成员,编译器回主动调用对象成员的默认无参构造大概全部参数都有默认值的有参
堆空间资源采取序次(类的对象-->对象成员)
4,静态数据成员
加上static修饰的数据成员
存储在全局/静态区,不占据对象的存储空间,静态数据成员被整个类的全部对象共享
规则:
4.1 private 的静态数据成员无法在类之外直接访问
4.2 初始化必须放在类外(一样平常紧接类界说
4.3 初始化时不能在前面+static,要加类名+作用域限定符
4.4 多条初始化序次和声明序次划一
4.5 对象访问 & 类名::访问
001const.cc
- #include <iostream>
- using std::cout;
- using std::endl;
- class Point{
- public:
- Point()
- //只能用 这种 严格意义的初始化
- /* :_ix(0),_iy(0) */
- {cout<<"Point()"<<endl;}
- Point(int x,int y=1)
- :_ix(x),_iy(y) //const int _ix=x;
- //can't _ix=x;
- {cout<<"Point(int ,int)"<<endl;}
- Point(const Point & p)
- :_ix(p._ix),_iy(p._iy)
- {cout<<"Point(Point &)"<<endl;}
- void print(){ cout<<_ix<<"\t\t"<<_iy<<endl; }
- private:
- const int _ix=1;
- const int _iy=1;
- //备胎中的备胎
- //只能算声明
- /* const int _ix; */
- /* const int _iy; */
- };
- void test(){
- Point pt1;
- Point pt2(5);
- Point pt(1,2);
- Point pt3(pt1);
- /* pt3=pt2; */
- //声明了常量数据成员,不能进行常规意义的赋值操作
- pt1.print();
- }
- int main(void)
- {
- test();
- return 0;
- }
复制代码 002refmember.cc
- #include <iostream>
- using std::cout;
- using std::endl;
- int gNum=100;
- //只能绑定一个已经存在,在引用期间成员的生命周期内
- //始终有效的变量(对象
- class Point{
- public:
- Point()
- :_ix(0),_iy(0)
- ,_ref(_ix)
- {cout<<"Point()"<<endl;}
- Point(int x,int y=1)
- :_ix(x),_iy(y) //const int _ix=x;
- /* ,_ref(_ix) */
- {cout<<"Point(int ,int)"<<endl;}
- /* Point(int x,int y,int z) */
- /* :_ix(x),_iy(y) //const int _ix=x; */
- /* ,_ref(z) */
- /* {cout<<"Point(int ,int,int)"<<endl;} */
- Point(const Point & p)
- :_ix(p._ix),_iy(p._iy)
- ,_ref(p._ref)
- {cout<<"Point(Point &)"<<endl;}
- void print(){ cout<<_ix<<"\t\t"<<_iy<<"\t\t"<<_ref<<endl; }
- private:
- int _ix;
- int _iy;
- /* int & _ref; */
- int & _ref=gNum;
- /* int & _ref=_iy; */
- };
- void test(){
- Point pt1;
- /* Point pt2(5,6,7); //7生命周期?一个牛逼的数字 */
- Point pt2(pt1);
- Point pt3(1,2);
- Point pt4(9);
- /* pt3=pt2; */
- //声明了常量数据成员,不能进行常规意义的赋值操作
- pt1.print();
- pt2.print();
- pt3.print();
- pt4.print();
- }
- int main(void)
- {
- test();
- return 0;
- }
复制代码 003object.cc
- #include <iostream>
- using std::cout;
- using std::endl;
- class Point{
- public:
- Point()
- :_ix(0),_iy(0)
- {cout<<"Point()"<<endl;}
- Point(int x=1,int y=1)
- :_ix(x),_iy(y) //const int _ix=x;
- {cout<<"Point(int ,int)"<<endl;}
- Point(const Point & p)
- :_ix(p._ix),_iy(p._iy)
- {cout<<"Point(Point &)"<<endl;}
- ~Point(){cout<<"~Point()"<<endl;}
- void print(){ cout<<_ix<<" "<<_iy; }
- private:
- int _ix;
- int _iy;
- };
- class Line{
- public:
- Line(int x1,int y1,int x2,int y2)
- :_pt1(x1,y1),_pt2(x2,y2)
- /* :_pt1(),_pt2() */
- //搞不懂啊啊啊,为撒不初始化的不行喵,创建worning,打印error
- //LIne的构造函数的初始化列表中
- //需要显示调用point的构造完成对象成员的初始化
- //如果没有,编译器自动调用POINT的无参构造
- //活着所有参数都有默认值的有参构造
- {cout<<"Line(int*4)"<<endl;}
- ~Line(){cout<<"~line"<<endl;}
- void printLine(){
- _pt1.print();
- cout<<"---->";
- _pt2.print();
- cout<<endl;
- }
- private:
- Point _pt1;
- Point _pt2;
- };
- void test(){
- Line l(1,2,3,4);
- l.printLine();
- /* Line l1(); */
- /* l1.printLine(); */
- }
- int main(void)
- {
- test();
- return 0;
- }
复制代码 04_static_member.cc
- #include <iostream>
- #include <string.h>
- using std::cout;
- using std::endl;
- class Computer{
- public:
- /* void init(const char* brand,int price){ */
- /* strcpy(_brand,brand); */
- /* _price=price; */
- /* } */
- void printBrandPrice(){
- cout<<_brand<<"\t";
- cout<<_price<<endl;
- }
- static int get_totalprice(){
- return _totalprice;
- }
- //----------------------------//
- /* private: */
- Computer(const char* brand,int price)
- : _brand(new char[strlen(brand)+1]())
- , _price(price)
- {
- strcpy(_brand,brand);// 否则没有赋值喵,不输出
- _totalprice+=_price;
- cout<<_brand<<"\t\t"<<"Computer(char* int)"<<endl;
- }
- Computer(const Computer & c)
- : _brand(new char[strlen(c._brand)+1]())
- , _price(c._price)
- {
- strcpy(_brand,c._brand);// 否则没有赋值喵,不输出
- _totalprice+=_price;
- cout<<"Computer(cosnt Computer &)"<<endl;
- }
- Computer & operator=(const Computer & c){
- if(this!=&c){
- delete [] _brand;
- _brand=new char[strlen(c._brand)+1]();
- strcpy(_brand,c._brand);
- _price=c._price;
- _totalprice+=_price;
- }
- return *this;
- }
- //----------------------------//
- ~Computer(){
- cout<<_brand<<"\t\t";
- if(_brand){
- delete _brand; //coution
- _brand=nullptr; //加上,信任行为喵
- _totalprice-=_price;
- }
- cout<<"~Computer"<<endl;
- }
- private:
- char* _brand;
- int _price;
- static int _totalprice;
- };
- int Computer::_totalprice=0;
- //----------------------------//
- void test(){
- cout<<sizeof(Computer)<<endl;
- cout<<Computer::get_totalprice()<<endl;
- Computer p1("xixi",66);
- cout<<p1.get_totalprice()<<endl;
- Computer p2("paxi",66);
- cout<<p1.get_totalprice()<<endl;
- }
- int main(void)
- {
- test();
- return 0;
- }
复制代码 05_constMember.cc
- #include <iostream>
- #include <string.h>
- using std::cout;
- using std::endl;
- class Computer{
- public:
- void init(int price)/*const*/{
- _price=price;
- }
- void printBrandPrice()const{
- cout<<_brand<<"\t";
- cout<<_price<<"\tconst_print"<<endl;
- }
- //----------------------------//
- /* private: */
- Computer(const char* brand,int price)
- : _brand(new char[strlen(brand)+1]())
- , _price(price)
- {
- strcpy(_brand,brand);// 否则没有赋值喵,不输出
- cout<<_brand<<"\t\t"<<"Computer(char* int)"<<endl;
- }
- Computer(const Computer & c)
- : _brand(new char[strlen(c._brand)+1]())
- , _price(c._price)
- {
- strcpy(_brand,c._brand);// 否则没有赋值喵,不输出
- cout<<"Computer(cosnt Computer &)"<<endl;
- }
- Computer & operator=(const Computer & c){
- if(this!=&c){
- delete [] _brand;
- _brand=new char[strlen(c._brand)+1]();
- strcpy(_brand,c._brand);
- _price=c._price;
- }
- return *this;
- }
- //----------------------------//
- ~Computer(){
- cout<<_brand<<"\t\t";
- if(_brand){
- delete _brand; //coution
- _brand=nullptr; //加上,信任行为喵
- }
- cout<<"~Computer"<<endl;
- }
- private:
- char* _brand;
- int _price;
- };
- //----------------------------//
- void test(){
- cout<<sizeof(Computer)<<endl;
- Computer p1("xixi",66);
- p1.init(7888);
- //不能修改对象的数据成员
- p1.printBrandPrice();
- cout<<endl;
- const Computer p2("paxi",66);
- p2.printBrandPrice();
- /* p2.init(7888); */
- cout<<endl;
- }
- int main(void)
- {
- test();
- return 0;
- }
复制代码 06_constfuction.cc
- #include <iostream>
- #include <string.h>
- using std::cout;
- using std::endl;
- class Computer{
- public:
- void init(int price)/*const*/{
- _price=price;
- }
- void printBrandPrice(const Computer & c)const{
- /* for(int i=0;i<strlen(c._brand)+1;i++){ */
- /* _brand[i]=c._brand[i]; */
- /* } */
- //尽量避免
- cout<<_brand<<"\t";
- cout<<_price<<"\tconst_print"<<endl;
- }
- //----------------------------//
- /* private: */
- Computer(const char* brand,int price)
- : _brand(new char[strlen(brand)+1]())
- , _price(price)
- {
- strcpy(_brand,brand);// 否则没有赋值喵,不输出
- cout<<_brand<<"\t\t"<<"Computer(char* int)"<<endl;
- }
- Computer(const Computer & c)
- : _brand(new char[strlen(c._brand)+1]())
- , _price(c._price)
- {
- strcpy(_brand,c._brand);// 否则没有赋值喵,不输出
- cout<<"Computer(cosnt Computer &)"<<endl;
- }
- Computer & operator=(const Computer & c){
- if(this!=&c){
- delete [] _brand;
- _brand=new char[strlen(c._brand)+1]();
- /* strcpy(_brand,c._brand); */
- _price=c._price;
- }
- return *this;
- }
- //----------------------------//
- ~Computer(){
- cout<<_brand<<"\t\t";
- if(_brand){
- delete _brand; //coution
- _brand=nullptr; //加上,信任行为喵
- }
- cout<<"~Computer"<<endl;
- }
- private:
- /* const char* _brand;//+brand[]也不行,只读变量不可赋值 */
- char* _brand;
- int _price;
- };
- //----------------------------//
- void test(){
- Computer p1("xixi",66);
- Computer p2("kaixin",66);
- p1.printBrandPrice(p2);
- cout<<endl;
- }
- int main(void)
- {
- test();
- return 0;
- }
复制代码 07_PointArray.cc
- #include <iostream>
- using std::cout;
- using std::endl;
- class Point{
- public:
- Point()
- :_ix(0),_iy(0)
- {cout<<"Point()"<<endl;}
- Point(int x/*=1*/,int y=1)
- :_ix(x),_iy(y) //const int _ix=x;
- {cout<<"Point(int ,int)"<<endl;}
- Point(const Point & p)
- :_ix(p._ix),_iy(p._iy)
- {cout<<"Point(Point &)"<<endl;}
- ~Point(){cout<<"~Point()"<<endl;}
- Point & operator=(const Point & p){
- _ix=p._ix;_iy=p._iy;
- return *this;
- }
- void print(){ cout<<_ix<<" "<<_iy<<endl; }
- private:
- int _ix;
- int _iy;
- };
- void test(){
- Point* p1=new Point(3,4);
- p1->print();
- (*p1).print();
- delete p1;
-
- //()确保进行了初始化,初始化为相应类信息的默认值
- int *p=new int();
- cout<<*p<<endl;
- delete p;
- //Point 类型的默认值是无参构造初始化的Point对象
- Point* p2=new Point();//调用模糊
- p2->print();
- delete p2;
- //------------------------------//
- cout<<endl;
- Point p4(7,5);
- Point p3(3,5);
- Point p6(6,5);
- Point arr[3]={p4,p3,p6};//Point(const Point &)
- cout<<&p4<<endl;
- cout<<arr<<endl;
- cout<<endl;
- Point ar1[2]={Point(1,2),Point(3,4)};
- ar1[0].print();
- (ar1+1)->print();
- cout<<endl;
- Point ar2[2]={{2,2},{5,5}};
- }
- int main(void)
- {
- test();
- return 0;
- }
复制代码 08_Student.cc
- #include <iostream>
- #include <string.h>
- using std::cout;
- using std::endl;
- class Student
- {
- public:
- Student(int id, const char * name)
- : _id(id)
- , _name(new char[strlen(name) + 1]())
- {
- strcpy(_name, name);
- cout << "Student()" << endl;
- }
- ~Student(){
- if(_name){
- delete [] _name;
- _name = nullptr;
- }
- }
- //------------------------------//
- void operator delete(void * pointer){
- cout<<"operator delete"<<endl;
- free(pointer);
- }
- void* operator new(size_t sz){
- cout<<"operator new "<<endl;
- void* ret=malloc(sz);
- return ret;
- }
- //------------------------------//
- void print() const{
- cout << "id:" << _id << "\t"<< "name:" << _name << endl;
- }
- private:
- int _id;
- char * _name;
- };
- //------------------------------//
- void test(){
- Student * s=new Student(3,"xixi");
- s->print();
- delete s;
- }
- int main(void)
- {
- test();
- return 0;
- }
复制代码 09_HeapStudent.cc
- #include <iostream>
- #include <string.h>
- using std::cout;
- using std::endl;
- class Student
- {
- public:
- Student(int id, const char * name)
- : _id(id)
- , _name(new char[strlen(name) + 1]())
- {
- strcpy(_name, name);
- cout << "Student()" << endl;
- }
- ~Student(){
- if(_name){
- delete [] _name;
- _name = nullptr;
- }
- }
- //------------------------------//
- void operator delete(void * pointer){
- cout<<"operator delete"<<endl;
- free(pointer);
- }
- void* operator new(size_t sz){
- cout<<"operator new "<<endl;
- void* ret=malloc(sz);
- return ret;
- }
- void destroy(){
- delete this;
- }
- //------------------------------//
- void print() const{
- cout << "id:" << _id << "\t"<< "name:" << _name << endl;
- }
- private:
- int _id;
- char * _name;
- };
- //------------------------------//
- void test(){
- Student * s=new Student(3,"xixi");
- s->print();
- s->destroy();
- /* delete s; */
- }
- int main(void)
- {
- test();
- return 0;
- }
复制代码 10_StackStudent.cc
- #include <iostream>
- #include <string.h>
- using std::cout;
- using std::endl;
- class Student
- {
- public:
- Student(int id, const char * name)
- : _id(id)
- , _name(new char[strlen(name) + 1]())
- {
- strcpy(_name, name);
- cout << "Student()" << endl;
- }
- ~Student(){
- if(_name){
- delete [] _name;
- _name = nullptr;
- }
- }
- void print() const{
- cout << "id:" << _id << "\t"<< "name:" << _name << endl;
- }
- private:
- //------------------------------//
- void operator delete(void * pointer){
- cout<<"operator delete"<<endl;
- free(pointer);
- }
- void* operator new(size_t sz){
- cout<<"operator new "<<endl;
- void* ret=malloc(sz);
- return ret;
- }
- void destroy(){
- delete this;
- }
- //------------------------------//
- private:
- int _id;
- char * _name;
- };
- //------------------------------//
- void test(){
- /* Student * s=new Student(3,"xixi"); */
- Student s(3,"xix");
- s.print();
- /* s.destroy(); */
- /* delete s; */
- }
- int main(void)
- {
- test();
- return 0;
- }
复制代码 static_single.cc
- #include <iostream>
- using std::cout;
- using std::endl;
- class Point{
- public:
- Point()
- :_ix(0),_iy(0)
- {cout<<"Point()"<<endl;}
- /* Point(const Point & p) */
- /* :_ix(p._ix),_iy(p._iy) */
- /* {cout<<"Point(Point &)"<<endl;} */
- //删除 拷贝构造效果更好?????
- //删除 赋值构造效果更好?????
- void print(){ cout<<_ix<<"\t\t"<<_iy<<endl; }
- static Point & getInstance(){
- static Point pt(2,8);
- //单例模式,只能初始化一次
- return pt;
- }//不是静态成员函数???
- private:
- Point(int x,int y=1)
- :_ix(x),_iy(y)
- {cout<<"Point(int ,int)"<<endl;}
- //构造 私有
- ~Point(){
- cout<<"~Point()"<<endl;
- }
- //析构 私有(四大金刚全部干掉喵
- private:
- int _ix;
- int _iy;
- };
- void test(){
- Point & pt1=Point::getInstance();
- Point & pt2=Point::getInstance();
- cout<<&pt1<<endl;
- cout<<&pt2<<endl;
- }
- int main(void)
- {
- test();
- return 0;
- }
复制代码 heap_single.cc
- #include <iostream>
- using std::cout;
- using std::endl;
- //堆上创建单例对象
- class Point{
- public:
- static Point * getInstance(){
- if(_pInstance==nullptr){
- _pInstance=new Point(1,2);
- }
- return _pInstance;
- }
- static void destroy(){
- if(_pInstance){
- delete _pInstance;
- _pInstance=nullptr;
- }
- cout<<"<<<delete heap"<<endl;
- }
- //C++11之后可以将成员函数从类中删除
- Point(Point & rhs)=delete;
- Point & operator=(const Point & rhs)=delete;
- void init(int x,int y){
- _ix=x;_iy=y;
- }//初始化和赋值分开
- void print(){ cout<<_ix<<"\t\t"<<_iy<<endl; }
- private:
- Point()
- :_ix(0),_iy(0)
- {cout<<"Point()"<<endl;}
- Point(int x,int y)
- :_ix(x),_iy(y) //const int _ix=x;
- {cout<<"Point(int ,int)"<<endl;}
- ~Point(){cout<<"~point()"<<endl;}
- private:
- int _ix;
- int _iy;
- static Point* _pInstance;
- //定义一个指针保存第一次创建的对象
- };
- Point * Point::_pInstance=nullptr;
- //初始化
- void test(){
- //使用规范,避免多个指针拥有单例对象的管理权
- Point::getInstance()->print();
- Point::getInstance()->init(21,13);
- Point::getInstance()->print();
- Point::getInstance()->init(23,231);
- Point::getInstance()->print();
- Point::destroy();
- Point::destroy();
- Point::destroy();
- //多次执行也不会有double free的问题
- Point::getInstance()->print();
- Point::destroy();
- }
- int main(void)
- {
- test();
- return 0;
- }
复制代码 06 作业
01,特殊的成员函数有哪几种?各自的特点是什么? (坏,答了一个特殊的数据成员
1,static成员函数
0.1 静态成员函数不依赖于某一对象
0.2 静态成员函数可以通过对象调用,但更加常见的方式是通过类名加上作用域限定符调用
0.3 静态成员函数没有this指针
0.4 静态成员函数中无法直接利用成员名访问非静态的成员,只能访问静态数据成员或调用静态成员(由于没有this指针),但好坏静态的成员函数可以访问静态成员
0.5 构造/析构/赋值运算符/拷贝构造比力特殊,可以在静态成员函数中调用(这四个函数不能设置为静态成员函数,他们会访问全部的成员对象,但是静态成员函数没有this指针)
2,const成员函数
0.1 cosnt 成员函数中,不能修改对象的数据成员
0.2 当编译器发现该函数是const成员函数时,会主动将this指针设置为双重const限定的指针
const 对象与const 成员函数的规则:
当类中有const成员函数和非const成员函数重载时,const对象会调用const成员函数,非const对象会调用非const成员函数
当类中只有一个const成员函数时,无论const对象还好坏const对象都可以调用这个版本
当类中只有一个非const成员函数时,const对象就不能调用非const版本
总结:假如一个成员函数中确定不会修改数据成员,就把它界说为const成员函数
02,写出以下步伐运行的结果。( )
- class Sample
- {
- public:
- Sample();
- void Display();
- private:
- int i;
- static int k;
- };
- Sample::Sample()
- {
- i=0;
- k++;
- }
- void Sample::Display()
- {
- cout << "i=" << i << ",k=" << k << endl;
- }
- int Sample::k=0;
- int main( )
- {
- Sample a, b;
- a.Display();
- b.Display();
-
- return 0;
- }
复制代码 i=0,k=1(2)坏
i=0,k=2
03,写出下面步伐的结果: 11
- #include <iostream>
- using std::cout;
- using std::endl;
- int i = 1;
- class Test
- {
- public:
- Test()
- :_first(i++) //1 2
- ,_second(i++)//2 3
- ,_third(i++)//3 4
- ,_fourth(_third)//3
- {
- _third = i;//4 _foruth=4
- }
- void print()
- {
- cout << "result : " << _first + _second + _third + _fourth << endl;
- //1 2 4 4 =11
- }
- private:
- int _first;
- int _second;
- int _third;
- int &_fourth;
- };
- int main()
- {
- Test test;
- test.print();
-
- return 0;
- }
复制代码 04,写出以下步伐运行的结果。
- #include <iostream>
- #include <math.h>
- using std::cout;
- using std::endl;
- class Point
- {
- public:
- Point(int xx = 0, int yy = 0)
- {
- X = xx;
- Y = yy;
- cout << "point构造函数被调用" << endl;
- }
-
- Point(Point &p);
-
- int GetX()
- {
- return X;
- }
-
- int GetY()
- {
- return Y;
- }
- private:
- int X,Y;
- };
- Point::Point(Point &p)
- {
- X = p.X;
- Y = p.Y;
- cout << "X = " << X << " Y=" << Y << "Point拷贝构造函数被调用" << endl;
- }
- class Distance
- {
- public:
- Distance(Point xp1, Point xp2);
- double GetDis()
- {
- return dist;
- }
- private:
- Point p1,p2;
- double dist;
- };
- //拷贝一次
- Distance::Distance(Point xp1, Point xp2)
- : p1(xp1)//拷贝一次
- , p2(xp2)
- {
- cout << "Distance构造函数被调用" << endl;
- double x = double(p1.GetX() - p2.GetX());
- double y = double(p1.GetY() - p2.GetY());
- dist = sqrt(x * x + y * y);
- }
- int main()
- {
- //point构造函数被调用
- //point构造函数被调用
- Point myp1(1,1), myp2(4,5);
- //X=1,Y=1,Point拷贝构造函数被调用
- //X=4,Y=5,Point拷贝构造函数被调用
- //X=1,Y=1,Point拷贝构造函数被调用
- //X=4,Y=5,Point拷贝构造函数被调用
- Distance myd(myp1, myp2);
- //Distance构造函数被调用
- cout << "The distance is:" ;
- //The distance is:dist
- cout << myd.GetDis() << endl;
-
- return 0;
- }
复制代码 point构造函数被调用
point构造函数被调用
X = 4 Y=5Point拷贝构造函数被调用
X = 1 Y=1Point拷贝构造函数被调用
X = 1 Y=1Point拷贝构造函数被调用
X = 4 Y=5Point拷贝构造函数被调用
Distance构造函数被调用
The distance is:5
05,写出以下步伐运行的结果。 122444
- #include <iostream>
- using std::cout;
- using std::endl;
- class MyClass
- {
- public:
- MyClass(int i = 0)
- {
- cout << i;
- }
- MyClass(const MyClass &x)
- {
- cout << 2;
- }
- MyClass &operator=(const MyClass &x)
- {
- cout << 3;
- return *this;
- }
- ~MyClass()
- {
- cout << 4;
- }
- };
- int main()
- {
- MyClass obj1(1), obj2(2);
- //12
- MyClass obj3 = obj1;//==Myclass obj3(obj1);
- //32444
- return 0;
- }
复制代码 06,不思量任何编译器优化(如:NRVO),下述代码的第10#会发生-fno-elide-constructors 编译时消除优化的影响
- #include <iostream>
- using std::cout;
- using std::endl;
- class B
- {
- public:
- B()
- {
- cout << "B()" << endl;
- }
- ~B()
- {
- cout << "~B()" << endl;
- }
-
- B(const B &rhs)
- {
- cout << "B(const B&)" << endl;
- }
-
- B &operator=(const B &rhs)
- {
- cout << "B &operator=(const B &s)" << endl;
-
- return *this;
- }
- };
- B func(const B &rhs)
- //cp 不是喵是赋值
- {
- cout << "B func(const B &)" << endl;
- return rhs;
- }
- int main(int argc, char **argv)
- {
- B b1,b2;
- b2=func(b1);//10#
- //B func(const B &)
- //B(const B&)
- //B &operator=(const B &s)
- return 0;
- }
复制代码 B func(const B &)
B(const B&)
B &operator=(const B &s)
07,new表达式和delete表达式的底层实现是什么?
1,对于自界说范例而言
利用new表达式的三个步调:
1,调用operator new尺度库函数申请未范例化的空间
2,在该空间上调用该范例的构造函数初始化对象
3,返回指向该对象的相应范例的指针
利用delete表达式···
1,调用析构函数,采取数据成员申请的资源(堆空间)
2,调用operator delete库函数采取本对象所在的空间
对于内置范例而言,则没有以上构造函数和析构函数实行的步调
08,实现单例的Point类(将单例对象创建在堆上)
heap_single.cc
09,实现一个单例的Computer类,包罗品牌和代价信息。
- #include <iostream>
- #include <string.h>
- using std::cout;
- using std::endl;
- class Computer{
- public:
- static Computer * get(){
- if(!_pp){
- _pp=new Computer("xixi",520);
- }
- return _pp;
- }
- /* void* operator new(size_t sz){ */
- /* void * ret=malloc(sz); */
- /* return ret; */
- /* } */
- /* void operator delete(void * p){ */
- /* free(p); */
- /* } */
- static void destory(){
- if(_pp){
- delete _pp;
- _pp=nullptr;
- }
- cout<<"<<<< destory"<<endl;
- }
- Computer(Computer & c)=delete;
- Computer & operator=(const Computer & c)=delete;
- void init(const char* brand,int price){
- strcpy(_brand,brand);
- _price=price;
- }
- void printBrandPrice(){
- cout<<_brand<<"\t";
- cout<<_price<<endl;
- }
- private:
- Computer(const char* brand,int price)
- : _brand(new char[strlen(brand)+1]())
- , _price(price)
- {
- strcpy(_brand,brand);// 否则没有赋值喵,不输出
- cout<<_brand<<"\t\t"<<"Computer"<<endl;
- }
- ~Computer(){
- cout<<_brand<<"\t\t";
- if(_brand){
- delete _brand; //coution
- _brand=nullptr; //加上,信任行为喵
- }
- cout<<"~Computer"<<endl;
- }
- private:
- char* _brand;
- int _price;
- static Computer * _pp;
- };
- Computer*Computer::_pp=nullptr;
- void test(){
- Computer::get()->printBrandPrice();
- Computer::get()->init("jiajia",7758258);
- Computer::get()->printBrandPrice();
- Computer::get()->init("yueyue",51930);
- Computer::get()->printBrandPrice();
- Computer::destory();
- Computer::destory();
- Computer::destory();
- }
- void test01(){
- }
- int main(void)
- {
- test();
- test01();
- return 0;
- }
复制代码 |