0816,特殊数据成员/成员函数,对象构造,delete/new,单例模式

[复制链接]
发表于 2026-1-30 09:30:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
01 特殊的数据成员 (是我写作业不看标题

1,常量数据成员
加上const 的数据成员,加上之后只能在声明和初始化队列中举行初始化,初始化之后不能举行赋值利用
2,引用数据数据成员
用引用方式创建的数据成员
引用数据成员须要在初始化列表中举行初始化
须要绑定一个已经存在的,在引用成员的生命周期内始终有效的变量(对象
3,对象成员
一个类的对象是其他类的数据成员
coution:
初始化列表写对象,不写类名,不能在声明对象成员时直接用有参构造创建
假如在初始化列表中没有表现的初始化对象成员,编译器回主动调用对象成员的默认无参构造大概全部参数都有默认值的有参
堆空间资源采取序次(类的对象-->对象成员)
4,静态数据成员
加上static修饰的数据成员
存储在全局/静态区,不占据对象的存储空间,静态数据成员被整个类的全部对象共享
规则:
4.1 private 的静态数据成员无法在类之外直接访问
4.2 初始化必须放在类外(一样平常紧接类界说
4.3 初始化时不能在前面+static,要加类名+作用域限定符
4.4 多条初始化序次和声明序次划一
4.5 对象访问 & 类名::访问
001const.cc
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4. class Point{
  5. public:
  6.     Point()
  7.         //只能用 这种 严格意义的初始化
  8.         /* :_ix(0),_iy(0) */
  9.         {cout<<"Point()"<<endl;}
  10.     Point(int x,int y=1)
  11.         :_ix(x),_iy(y) //const int _ix=x;
  12.                        //can't _ix=x;
  13.         {cout<<"Point(int ,int)"<<endl;}
  14.     Point(const Point & p)
  15.         :_ix(p._ix),_iy(p._iy)
  16.         {cout<<"Point(Point &)"<<endl;}
  17.     void print(){ cout<<_ix<<"\t\t"<<_iy<<endl; }
  18. private:
  19.    const  int _ix=1;
  20.    const  int _iy=1;
  21.    //备胎中的备胎
  22.    //只能算声明
  23.    /* const  int _ix; */
  24.    /* const  int _iy; */
  25. };
  26. void test(){
  27.     Point pt1;
  28.     Point pt2(5);
  29.     Point pt(1,2);
  30.     Point pt3(pt1);
  31.     /* pt3=pt2; */
  32.     //声明了常量数据成员,不能进行常规意义的赋值操作
  33.     pt1.print();
  34. }
  35. int main(void)
  36. {
  37.     test();
  38.     return 0;
  39. }
复制代码
002refmember.cc
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4. int gNum=100;
  5. //只能绑定一个已经存在,在引用期间成员的生命周期内
  6. //始终有效的变量(对象
  7. class Point{
  8. public:
  9.     Point()
  10.         :_ix(0),_iy(0)
  11.          ,_ref(_ix)
  12.         {cout<<"Point()"<<endl;}
  13.     Point(int x,int y=1)
  14.         :_ix(x),_iy(y) //const int _ix=x;
  15.         /* ,_ref(_ix) */
  16.         {cout<<"Point(int ,int)"<<endl;}
  17.     /* Point(int x,int y,int z) */
  18.     /*     :_ix(x),_iy(y) //const int _ix=x; */
  19.     /*     ,_ref(z) */
  20.     /*     {cout<<"Point(int ,int,int)"<<endl;} */
  21.     Point(const Point & p)
  22.         :_ix(p._ix),_iy(p._iy)
  23.          ,_ref(p._ref)
  24.         {cout<<"Point(Point &)"<<endl;}
  25.     void print(){ cout<<_ix<<"\t\t"<<_iy<<"\t\t"<<_ref<<endl; }
  26. private:
  27.     int _ix;
  28.     int _iy;
  29.     /* int & _ref; */
  30.     int & _ref=gNum;
  31.     /* int & _ref=_iy; */
  32. };
  33. void test(){
  34.     Point pt1;
  35.     /* Point pt2(5,6,7); //7生命周期?一个牛逼的数字 */
  36.     Point pt2(pt1);
  37.     Point pt3(1,2);
  38.     Point pt4(9);
  39.     /* pt3=pt2; */
  40.     //声明了常量数据成员,不能进行常规意义的赋值操作
  41.     pt1.print();
  42.     pt2.print();
  43.     pt3.print();
  44.     pt4.print();
  45. }
  46. int main(void)
  47. {
  48.     test();
  49.     return 0;
  50. }
复制代码
 003object.cc
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4. class Point{
  5. public:
  6.     Point()
  7.         :_ix(0),_iy(0)
  8.         {cout<<"Point()"<<endl;}
  9.     Point(int x=1,int y=1)
  10.         :_ix(x),_iy(y) //const int _ix=x;
  11.         {cout<<"Point(int ,int)"<<endl;}
  12.     Point(const Point & p)
  13.         :_ix(p._ix),_iy(p._iy)
  14.         {cout<<"Point(Point &)"<<endl;}
  15.     ~Point(){cout<<"~Point()"<<endl;}
  16.     void print(){ cout<<_ix<<" "<<_iy; }
  17. private:
  18.     int _ix;
  19.     int _iy;
  20. };
  21. class Line{
  22. public:
  23.     Line(int x1,int y1,int x2,int y2)
  24.         :_pt1(x1,y1),_pt2(x2,y2)
  25.         /* :_pt1(),_pt2() */
  26.          //搞不懂啊啊啊,为撒不初始化的不行喵,创建worning,打印error
  27.          //LIne的构造函数的初始化列表中
  28.          //需要显示调用point的构造完成对象成员的初始化
  29.          //如果没有,编译器自动调用POINT的无参构造
  30.          //活着所有参数都有默认值的有参构造
  31.         {cout<<"Line(int*4)"<<endl;}
  32.     ~Line(){cout<<"~line"<<endl;}
  33.     void printLine(){
  34.         _pt1.print();
  35.         cout<<"---->";
  36.         _pt2.print();
  37.         cout<<endl;
  38.     }
  39. private:
  40.          Point _pt1;
  41.          Point _pt2;
  42. };
  43. void test(){
  44.     Line l(1,2,3,4);
  45.     l.printLine();
  46.     /* Line l1(); */
  47.     /* l1.printLine(); */
  48. }
  49. int main(void)
  50. {
  51.     test();
  52.     return 0;
  53. }
复制代码
04_static_member.cc 
  1. #include <iostream>
  2. #include <string.h>
  3. using std::cout;
  4. using std::endl;
  5. class Computer{
  6. public:
  7.     /* void init(const char* brand,int price){ */
  8.     /*     strcpy(_brand,brand); */
  9.     /*     _price=price; */
  10.     /* } */
  11.     void printBrandPrice(){
  12.         cout<<_brand<<"\t";
  13.         cout<<_price<<endl;
  14.     }
  15.     static int get_totalprice(){
  16.         return _totalprice;
  17.     }
  18. //----------------------------//
  19. /* private: */
  20.     Computer(const char* brand,int price)
  21.         : _brand(new char[strlen(brand)+1]())
  22.         , _price(price)
  23.     {
  24.         strcpy(_brand,brand);// 否则没有赋值喵,不输出
  25.         _totalprice+=_price;
  26.        cout<<_brand<<"\t\t"<<"Computer(char* int)"<<endl;
  27.     }
  28.     Computer(const Computer & c)
  29.         : _brand(new char[strlen(c._brand)+1]())
  30.         , _price(c._price)
  31.     {
  32.         strcpy(_brand,c._brand);// 否则没有赋值喵,不输出
  33.         _totalprice+=_price;
  34.        cout<<"Computer(cosnt Computer &)"<<endl;
  35.     }
  36.     Computer & operator=(const Computer & c){
  37.         if(this!=&c){
  38.             delete [] _brand;
  39.             _brand=new char[strlen(c._brand)+1]();
  40.             strcpy(_brand,c._brand);
  41.             _price=c._price;
  42.             _totalprice+=_price;
  43.         }
  44.         return *this;
  45.     }
  46. //----------------------------//
  47.     ~Computer(){
  48.         cout<<_brand<<"\t\t";
  49.         if(_brand){
  50.             delete  _brand;  //coution
  51.              _brand=nullptr;   //加上,信任行为喵
  52.             _totalprice-=_price;
  53.         }
  54.         cout<<"~Computer"<<endl;
  55.     }
  56. private:
  57.     char* _brand;
  58.     int _price;
  59.     static int  _totalprice;
  60. };
  61. int Computer::_totalprice=0;
  62. //----------------------------//
  63. void test(){
  64.     cout<<sizeof(Computer)<<endl;
  65.     cout<<Computer::get_totalprice()<<endl;
  66.     Computer p1("xixi",66);
  67.     cout<<p1.get_totalprice()<<endl;
  68.     Computer p2("paxi",66);
  69.     cout<<p1.get_totalprice()<<endl;
  70. }
  71. int main(void)
  72. {
  73.     test();
  74.     return 0;
  75. }
复制代码
05_constMember.cc
  1. #include <iostream>
  2. #include <string.h>
  3. using std::cout;
  4. using std::endl;
  5. class Computer{
  6. public:
  7.     void init(int price)/*const*/{
  8.         _price=price;
  9.     }
  10.     void printBrandPrice()const{
  11.         cout<<_brand<<"\t";
  12.         cout<<_price<<"\tconst_print"<<endl;
  13.     }
  14. //----------------------------//
  15. /* private: */
  16.     Computer(const char* brand,int price)
  17.         : _brand(new char[strlen(brand)+1]())
  18.         , _price(price)
  19.     {
  20.         strcpy(_brand,brand);// 否则没有赋值喵,不输出
  21.        cout<<_brand<<"\t\t"<<"Computer(char* int)"<<endl;
  22.     }
  23.     Computer(const Computer & c)
  24.         : _brand(new char[strlen(c._brand)+1]())
  25.         , _price(c._price)
  26.     {
  27.         strcpy(_brand,c._brand);// 否则没有赋值喵,不输出
  28.        cout<<"Computer(cosnt Computer &)"<<endl;
  29.     }
  30.     Computer & operator=(const Computer & c){
  31.         if(this!=&c){
  32.             delete [] _brand;
  33.             _brand=new char[strlen(c._brand)+1]();
  34.             strcpy(_brand,c._brand);
  35.             _price=c._price;
  36.         }
  37.         return *this;
  38.     }
  39. //----------------------------//
  40.     ~Computer(){
  41.         cout<<_brand<<"\t\t";
  42.         if(_brand){
  43.             delete  _brand;  //coution
  44.              _brand=nullptr;   //加上,信任行为喵
  45.         }
  46.         cout<<"~Computer"<<endl;
  47.     }
  48. private:
  49.     char* _brand;
  50.     int _price;
  51. };
  52. //----------------------------//
  53. void test(){
  54.     cout<<sizeof(Computer)<<endl;
  55.     Computer p1("xixi",66);
  56.     p1.init(7888);
  57.     //不能修改对象的数据成员
  58.     p1.printBrandPrice();
  59.     cout<<endl;
  60.     const Computer p2("paxi",66);
  61.     p2.printBrandPrice();
  62.     /* p2.init(7888); */
  63.     cout<<endl;
  64. }
  65. int main(void)
  66. {
  67.     test();
  68.     return 0;
  69. }
复制代码
06_constfuction.cc
  1. #include <iostream>
  2. #include <string.h>
  3. using std::cout;
  4. using std::endl;
  5. class Computer{
  6. public:
  7.     void init(int price)/*const*/{
  8.         _price=price;
  9.     }
  10.     void printBrandPrice(const Computer & c)const{
  11.         /* for(int i=0;i<strlen(c._brand)+1;i++){ */
  12.         /*     _brand[i]=c._brand[i]; */
  13.         /* } */
  14.         //尽量避免
  15.         cout<<_brand<<"\t";
  16.         cout<<_price<<"\tconst_print"<<endl;
  17.     }
  18.     //----------------------------//
  19.     /* private: */
  20.     Computer(const char* brand,int price)
  21.         : _brand(new char[strlen(brand)+1]())
  22.           , _price(price)
  23.     {
  24.         strcpy(_brand,brand);// 否则没有赋值喵,不输出
  25.         cout<<_brand<<"\t\t"<<"Computer(char* int)"<<endl;
  26.     }
  27.     Computer(const Computer & c)
  28.         : _brand(new char[strlen(c._brand)+1]())
  29.           , _price(c._price)
  30.     {
  31.         strcpy(_brand,c._brand);// 否则没有赋值喵,不输出
  32.         cout<<"Computer(cosnt Computer &)"<<endl;
  33.     }
  34.     Computer & operator=(const Computer & c){
  35.         if(this!=&c){
  36.             delete [] _brand;
  37.             _brand=new char[strlen(c._brand)+1]();
  38.             /* strcpy(_brand,c._brand); */
  39.             _price=c._price;
  40.         }
  41.         return *this;
  42.     }
  43.     //----------------------------//
  44.     ~Computer(){
  45.         cout<<_brand<<"\t\t";
  46.         if(_brand){
  47.             delete  _brand;  //coution
  48.             _brand=nullptr;   //加上,信任行为喵
  49.         }
  50.         cout<<"~Computer"<<endl;
  51.     }
  52. private:
  53.     /* const char* _brand;//+brand[]也不行,只读变量不可赋值 */
  54.     char* _brand;
  55.     int _price;
  56. };
  57. //----------------------------//
  58. void test(){
  59.     Computer p1("xixi",66);
  60.     Computer p2("kaixin",66);
  61.     p1.printBrandPrice(p2);
  62.     cout<<endl;
  63. }
  64. int main(void)
  65. {
  66.     test();
  67.     return 0;
  68. }
复制代码
07_PointArray.cc
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4. class Point{
  5. public:
  6.     Point()
  7.         :_ix(0),_iy(0)
  8.         {cout<<"Point()"<<endl;}
  9.     Point(int x/*=1*/,int y=1)
  10.         :_ix(x),_iy(y) //const int _ix=x;
  11.         {cout<<"Point(int ,int)"<<endl;}
  12.     Point(const Point & p)
  13.         :_ix(p._ix),_iy(p._iy)
  14.         {cout<<"Point(Point &)"<<endl;}
  15.     ~Point(){cout<<"~Point()"<<endl;}
  16.     Point & operator=(const Point & p){
  17.         _ix=p._ix;_iy=p._iy;
  18.         return *this;
  19.     }
  20.     void print(){ cout<<_ix<<" "<<_iy<<endl; }
  21. private:
  22.     int _ix;
  23.     int _iy;
  24. };
  25. void test(){
  26.     Point* p1=new Point(3,4);
  27.     p1->print();
  28.     (*p1).print();
  29.     delete p1;
  30.    
  31.     //()确保进行了初始化,初始化为相应类信息的默认值
  32.     int *p=new int();
  33.     cout<<*p<<endl;
  34.     delete p;
  35.     //Point 类型的默认值是无参构造初始化的Point对象
  36.     Point* p2=new Point();//调用模糊
  37.     p2->print();
  38.     delete p2;
  39.     //------------------------------//
  40.     cout<<endl;
  41.     Point p4(7,5);
  42.     Point p3(3,5);
  43.     Point p6(6,5);
  44.     Point arr[3]={p4,p3,p6};//Point(const Point &)
  45.     cout<<&p4<<endl;
  46.     cout<<arr<<endl;
  47.     cout<<endl;
  48.     Point ar1[2]={Point(1,2),Point(3,4)};
  49.     ar1[0].print();
  50.     (ar1+1)->print();
  51.     cout<<endl;
  52.     Point ar2[2]={{2,2},{5,5}};
  53. }
  54. int main(void)
  55. {
  56.     test();
  57.     return 0;
  58. }
复制代码
08_Student.cc
  1. #include <iostream>
  2. #include <string.h>
  3. using std::cout;
  4. using std::endl;
  5. class Student
  6. {
  7. public:
  8.         Student(int id, const char * name)
  9.         : _id(id)
  10.         , _name(new char[strlen(name) + 1]())
  11.         {
  12.                 strcpy(_name, name);
  13.                 cout << "Student()" << endl;
  14.         }
  15.         ~Student(){
  16.         if(_name){
  17.             delete [] _name;
  18.             _name = nullptr;
  19.         }
  20.         }
  21.     //------------------------------//
  22.     void operator delete(void * pointer){
  23.         cout<<"operator delete"<<endl;
  24.         free(pointer);
  25.     }
  26.     void* operator new(size_t sz){
  27.         cout<<"operator new "<<endl;
  28.         void* ret=malloc(sz);
  29.         return ret;
  30.     }
  31.     //------------------------------//
  32.         void print() const{
  33.                 cout << "id:" << _id << "\t"<< "name:" << _name << endl;
  34.         }
  35. private:
  36.         int _id;
  37.         char * _name;
  38. };
  39. //------------------------------//
  40. void test(){
  41.     Student * s=new Student(3,"xixi");
  42.     s->print();
  43.     delete s;
  44. }
  45. int main(void)
  46. {
  47.     test();
  48.     return 0;
  49. }
复制代码
09_HeapStudent.cc
  1. #include <iostream>
  2. #include <string.h>
  3. using std::cout;
  4. using std::endl;
  5. class Student
  6. {
  7. public:
  8.         Student(int id, const char * name)
  9.         : _id(id)
  10.         , _name(new char[strlen(name) + 1]())
  11.         {
  12.                 strcpy(_name, name);
  13.                 cout << "Student()" << endl;
  14.         }
  15.         ~Student(){
  16.         if(_name){
  17.             delete [] _name;
  18.             _name = nullptr;
  19.         }
  20.         }
  21.     //------------------------------//
  22.     void operator delete(void * pointer){
  23.         cout<<"operator delete"<<endl;
  24.         free(pointer);
  25.     }
  26.     void* operator new(size_t sz){
  27.         cout<<"operator new "<<endl;
  28.         void* ret=malloc(sz);
  29.         return ret;
  30.     }
  31.     void destroy(){
  32.         delete this;
  33.     }
  34.     //------------------------------//
  35.         void print() const{
  36.                 cout << "id:" << _id << "\t"<< "name:" << _name << endl;
  37.         }
  38. private:
  39.         int _id;
  40.         char * _name;
  41. };
  42. //------------------------------//
  43. void test(){
  44.     Student * s=new Student(3,"xixi");
  45.     s->print();
  46.     s->destroy();
  47.     /* delete s; */
  48. }
  49. int main(void)
  50. {
  51.     test();
  52.     return 0;
  53. }
复制代码
10_StackStudent.cc
  1. #include <iostream>
  2. #include <string.h>
  3. using std::cout;
  4. using std::endl;
  5. class Student
  6. {
  7. public:
  8.         Student(int id, const char * name)
  9.         : _id(id)
  10.         , _name(new char[strlen(name) + 1]())
  11.         {
  12.                 strcpy(_name, name);
  13.                 cout << "Student()" << endl;
  14.         }
  15.         ~Student(){
  16.         if(_name){
  17.             delete [] _name;
  18.             _name = nullptr;
  19.         }
  20.         }
  21.         void print() const{
  22.                 cout << "id:" << _id << "\t"<< "name:" << _name << endl;
  23.         }
  24. private:
  25.     //------------------------------//
  26.     void operator delete(void * pointer){
  27.         cout<<"operator delete"<<endl;
  28.         free(pointer);
  29.     }
  30.     void* operator new(size_t sz){
  31.         cout<<"operator new "<<endl;
  32.         void* ret=malloc(sz);
  33.         return ret;
  34.     }
  35.     void destroy(){
  36.         delete this;
  37.     }
  38.     //------------------------------//
  39. private:
  40.         int _id;
  41.         char * _name;
  42. };
  43. //------------------------------//
  44. void test(){
  45.     /* Student * s=new Student(3,"xixi"); */
  46.     Student s(3,"xix");
  47.     s.print();
  48.     /* s.destroy(); */
  49.     /* delete s; */
  50. }
  51. int main(void)
  52. {
  53.     test();
  54.     return 0;
  55. }
复制代码
static_single.cc
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4. class Point{
  5. public:
  6.     Point()
  7.         :_ix(0),_iy(0)
  8.         {cout<<"Point()"<<endl;}
  9.     /* Point(const Point & p) */
  10.     /*     :_ix(p._ix),_iy(p._iy) */
  11.     /*     {cout<<"Point(Point &)"<<endl;} */
  12.     //删除  拷贝构造效果更好?????
  13.     //删除  赋值构造效果更好?????
  14.     void print(){ cout<<_ix<<"\t\t"<<_iy<<endl; }
  15.     static Point & getInstance(){
  16.         static Point pt(2,8);
  17.         //单例模式,只能初始化一次
  18.         return pt;
  19.     }//不是静态成员函数???
  20. private:
  21.     Point(int x,int y=1)
  22.         :_ix(x),_iy(y)
  23.         {cout<<"Point(int ,int)"<<endl;}
  24.     //构造  私有
  25.     ~Point(){
  26.         cout<<"~Point()"<<endl;
  27.     }
  28.     //析构  私有(四大金刚全部干掉喵
  29. private:
  30.     int _ix;
  31.     int _iy;
  32. };
  33. void test(){
  34.     Point & pt1=Point::getInstance();
  35.     Point & pt2=Point::getInstance();
  36.     cout<<&pt1<<endl;
  37.     cout<<&pt2<<endl;
  38. }
  39. int main(void)
  40. {
  41.     test();
  42.     return 0;
  43. }
复制代码
heap_single.cc 
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4. //堆上创建单例对象
  5. class Point{
  6. public:
  7.     static Point * getInstance(){
  8.         if(_pInstance==nullptr){
  9.             _pInstance=new Point(1,2);
  10.         }
  11.         return _pInstance;
  12.     }
  13.     static void destroy(){
  14.         if(_pInstance){
  15.             delete _pInstance;
  16.             _pInstance=nullptr;
  17.         }
  18.         cout<<"<<<delete heap"<<endl;
  19.     }
  20.     //C++11之后可以将成员函数从类中删除
  21.     Point(Point & rhs)=delete;
  22.     Point & operator=(const Point & rhs)=delete;
  23.     void init(int x,int y){
  24.         _ix=x;_iy=y;
  25.     }//初始化和赋值分开
  26.     void print(){ cout<<_ix<<"\t\t"<<_iy<<endl; }
  27. private:
  28.     Point()
  29.         :_ix(0),_iy(0)
  30.         {cout<<"Point()"<<endl;}
  31.     Point(int x,int y)
  32.         :_ix(x),_iy(y) //const int _ix=x;
  33.         {cout<<"Point(int ,int)"<<endl;}
  34.     ~Point(){cout<<"~point()"<<endl;}
  35. private:
  36.    int _ix;
  37.    int _iy;
  38.    static Point* _pInstance;
  39.    //定义一个指针保存第一次创建的对象
  40. };
  41. Point * Point::_pInstance=nullptr;
  42. //初始化
  43. void test(){
  44.     //使用规范,避免多个指针拥有单例对象的管理权
  45.     Point::getInstance()->print();
  46.     Point::getInstance()->init(21,13);
  47.     Point::getInstance()->print();
  48.     Point::getInstance()->init(23,231);
  49.     Point::getInstance()->print();
  50.     Point::destroy();
  51.     Point::destroy();
  52.     Point::destroy();
  53.     //多次执行也不会有double free的问题
  54.     Point::getInstance()->print();
  55.     Point::destroy();
  56. }
  57. int main(void)
  58. {
  59.     test();
  60.     return 0;
  61. }
复制代码
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,写出以下步伐运行的结果。( )
  1. class Sample
  2. {
  3. public:
  4.       Sample();
  5.       void Display();
  6. private:
  7.       int i;
  8.       static int k;
  9. };
  10. Sample::Sample()
  11. {
  12.         i=0;
  13.         k++;
  14. }
  15. void Sample::Display()
  16. {
  17.    cout << "i=" << i << ",k=" << k << endl;
  18. }
  19. int Sample::k=0;
  20. int main( )
  21. {
  22.     Sample a, b;
  23.     a.Display();
  24.     b.Display();
  25.    
  26.     return 0;
  27. }
复制代码
i=0,k=1(2)坏
i=0,k=2 
03,写出下面步伐的结果:  11
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4. int i = 1;
  5. class Test
  6. {
  7. public:
  8.         Test()
  9.         :_first(i++) //1  2
  10.         ,_second(i++)//2  3
  11.         ,_third(i++)//3  4
  12.         ,_fourth(_third)//3
  13.         {
  14.                 _third = i;//4   _foruth=4
  15.         }
  16.         void print()
  17.         {  
  18.                 cout << "result : " << _first + _second + _third + _fourth << endl;
  19.         //1 2 4 4 =11
  20.         }
  21. private:
  22.         int _first;
  23.         int _second;
  24.         int _third;
  25.         int &_fourth;
  26. };
  27. int main()
  28. {
  29.         Test test;
  30.         test.print();
  31.        
  32.         return 0;
  33. }
复制代码
04,写出以下步伐运行的结果。
  1. #include <iostream>
  2. #include <math.h>
  3. using std::cout;
  4. using std::endl;
  5. class Point       
  6. {
  7. public:
  8.     Point(int xx = 0, int yy = 0)
  9.         {
  10.                 X = xx;
  11.                 Y = yy;
  12.                 cout << "point构造函数被调用" << endl;
  13.         }
  14.        
  15.            Point(Point &p);
  16.           
  17.            int GetX()
  18.            {
  19.                    return X;
  20.            }
  21.           
  22.            int GetY()
  23.            {
  24.                    return Y;
  25.            }
  26. private:
  27.         int X,Y;
  28. };
  29. Point::Point(Point &p)       
  30. {
  31.         X = p.X;
  32.         Y = p.Y;
  33.         cout << "X = " << X << " Y=" << Y << "Point拷贝构造函数被调用" << endl;
  34. }
  35. class Distance       
  36. {
  37. public:       
  38.         Distance(Point xp1, Point xp2);
  39.         double GetDis()
  40.         {
  41.                 return dist;
  42.         }
  43. private:       
  44.         Point p1,p2;
  45.         double dist;       
  46. };
  47. //拷贝一次
  48. Distance::Distance(Point xp1, Point xp2)
  49. : p1(xp1)//拷贝一次
  50. , p2(xp2)
  51. {
  52.         cout << "Distance构造函数被调用" << endl;
  53.         double x = double(p1.GetX() - p2.GetX());
  54.         double y = double(p1.GetY() - p2.GetY());
  55.         dist = sqrt(x * x + y * y);
  56. }
  57. int main()
  58. {
  59.     //point构造函数被调用
  60.     //point构造函数被调用
  61.         Point myp1(1,1), myp2(4,5);
  62.     //X=1,Y=1,Point拷贝构造函数被调用
  63.     //X=4,Y=5,Point拷贝构造函数被调用
  64.     //X=1,Y=1,Point拷贝构造函数被调用
  65.     //X=4,Y=5,Point拷贝构造函数被调用
  66.         Distance myd(myp1, myp2);
  67.     //Distance构造函数被调用
  68.         cout << "The distance is:" ;
  69.     //The distance is:dist
  70.         cout << myd.GetDis() << endl;
  71.        
  72.         return 0;
  73. }
复制代码
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
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4. class MyClass
  5. {
  6. public:
  7.     MyClass(int i = 0)
  8.     {
  9.         cout << i;
  10.     }
  11.     MyClass(const MyClass &x)
  12.     {
  13.         cout << 2;
  14.     }
  15.     MyClass &operator=(const MyClass &x)
  16.     {
  17.         cout << 3;
  18.         return *this;
  19.     }
  20.     ~MyClass()
  21.     {
  22.         cout << 4;
  23.     }
  24. };
  25. int main()
  26. {
  27.     MyClass obj1(1), obj2(2);
  28.     //12
  29.     MyClass obj3 = obj1;//==Myclass obj3(obj1);
  30.     //32444
  31.     return 0;
  32. }
复制代码
06,不思量任何编译器优化(如:NRVO),下述代码的第10#会发生-fno-elide-constructors 编译时消除优化的影响
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4. class B
  5. {
  6. public:
  7.         B()
  8.         {
  9.         cout << "B()" << endl;
  10.     }
  11.     ~B()
  12.     {
  13.             cout << "~B()" << endl;
  14.     }
  15.    
  16.     B(const B &rhs)
  17.     {
  18.         cout << "B(const B&)" << endl;
  19.     }
  20.    
  21.     B &operator=(const B &rhs)
  22.     {
  23.             cout << "B &operator=(const B &s)" << endl;
  24.    
  25.         return  *this;
  26.     }
  27. };
  28. B func(const B &rhs)
  29.     //cp  不是喵是赋值
  30. {
  31.     cout << "B func(const B &)" << endl;
  32.     return rhs;
  33. }
  34. int main(int argc, char **argv)
  35. {
  36.         B b1,b2;
  37.     b2=func(b1);//10#
  38.                 //B func(const B &)
  39.                 //B(const B&)
  40.                 //B &operator=(const B &s)
  41.         return 0;
  42. }
复制代码
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类,包罗品牌和代价信息。
  1. #include <iostream>
  2. #include <string.h>
  3. using std::cout;
  4. using std::endl;
  5. class Computer{
  6. public:
  7.     static Computer * get(){
  8.         if(!_pp){
  9.            _pp=new Computer("xixi",520);
  10.         }
  11.         return _pp;
  12.     }
  13.     /* void* operator new(size_t sz){ */
  14.     /*     void * ret=malloc(sz); */
  15.     /*     return ret; */
  16.     /* } */
  17.     /* void operator delete(void * p){ */
  18.     /*     free(p); */
  19.     /* } */
  20.     static void destory(){
  21.         if(_pp){
  22.             delete _pp;
  23.             _pp=nullptr;
  24.         }
  25.         cout<<"<<<< destory"<<endl;
  26.     }
  27.     Computer(Computer & c)=delete;
  28.     Computer & operator=(const Computer & c)=delete;
  29.     void init(const char* brand,int price){
  30.         strcpy(_brand,brand);
  31.         _price=price;
  32.     }
  33.     void printBrandPrice(){
  34.         cout<<_brand<<"\t";
  35.         cout<<_price<<endl;
  36.     }
  37. private:
  38.     Computer(const char* brand,int price)
  39.         : _brand(new char[strlen(brand)+1]())
  40.         , _price(price)
  41.     {
  42.         strcpy(_brand,brand);// 否则没有赋值喵,不输出
  43.        cout<<_brand<<"\t\t"<<"Computer"<<endl;
  44.     }
  45.     ~Computer(){
  46.         cout<<_brand<<"\t\t";
  47.         if(_brand){
  48.             delete  _brand;  //coution
  49.              _brand=nullptr;   //加上,信任行为喵
  50.         }
  51.         cout<<"~Computer"<<endl;
  52.     }
  53. private:
  54.     char* _brand;
  55.     int _price;
  56.     static Computer * _pp;
  57. };
  58. Computer*Computer::_pp=nullptr;
  59. void test(){
  60.     Computer::get()->printBrandPrice();
  61.     Computer::get()->init("jiajia",7758258);
  62.     Computer::get()->printBrandPrice();
  63.     Computer::get()->init("yueyue",51930);
  64.     Computer::get()->printBrandPrice();
  65.     Computer::destory();
  66.     Computer::destory();
  67.     Computer::destory();
  68. }
  69. void test01(){
  70. }
  71. int main(void)
  72. {
  73.     test();
  74.     test01();
  75.     return 0;
  76. }
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表