打赏

5.5-day05-C++单目操作符/继承

二、
 
2.+=/-=/*=...
左变右不变。
表达式的值是左值,左操作数的引用。
(a += b) = c;
 
3.<</>>
int i = 10;
float f = 1.23;
Complex c (...);
cout << c << i << f << endl;
cin >> c;
左操作数ostream/istream类型,不能是常量,不能拷贝。
右操作数自定义类型,对于<<可以是常量,对于>>不能是常量。
表达式的值是左操作数的引用。
::operator<< (cout, c).operator<< (i).operator<< (f).operator<< (endl);
 
三、单目操作符
1.-(取负)/!/~
操作数不变。
表达式的值是右值。
 
2.前++/前--
操作数变。
表达式的值是运算以后的值。
表达式的值是左值,操作数的引用。
(++i) = 100;
++++++i;
 
3.后++/后--
操作数变。
表达式的值是运算以前的值。
表达式的值是右值。
 
四、其它操作符
1.下标操作符:[]
int arr[10] = { ... };
arr[1] = 10;
cout << arr[1] << endl;
-----------------------
class Array { ... };
Array arr (...);
arr[1] = 10;
cout << arr[1] << endl;
双目操作符,左操作数是一个具有容器特性的对象,右操作数是容器中特定数据元素的索引(基零的下标)。
下标表达式的值可以是左值,也可以是右值,由容器对象的常属性决定。常容器下标表达式的值是右值,非常容器下标表达式的值是左值。
 
2.函数操作符:()
如果为一个类定义了形如:
返回类型 operator() (形参表) {...}
的操作符函数,那么这个类所实例化的对象就可以被当做函数使用。
 
3.解引用(*)和间接访问(->)操作符
以指针的方式使用类类型的对象。
 
4.自定义类型转换和类型转换操作符
1)通过单参构造实现自定义类型转换
如果A类中有一个可以接受B类对象做为唯一参数的构造函数,那么B类型的对象就可以根据该构造函数被转换为A类型。
通过explicit关键字,可以强制使用该构造函数所完成的类型转换必须显示进行。
 
2)通过类型转换操作符函数实现自定义类型转换
如果A类中有一个形如
operator B (void) const { ... }
的操作符函数,那么A类型的对象就可以根据该函数被转换为B类型。
 
3)如果目标类型是类类型,源类型是基本类型,那么就只能通过在目标类型中定义以源类型为单参的构造函数实现类型转换。如果目标类型是基本类型,源类型是类类型,那么就只能通过在源类型中定义以目标类型为函数名的类型转换操作符函数实现类型转换。如果目标类型和源类型都是类类型,那么以上两种方法任取其一,但是不能同时使用。如果目标类型和源类型都是基本类型,那么无法实现自定义类型转换。
 
5.new/delete操作符
 
1.cpp
  1. #include<iostream>
  2. usingnamespace std;
  3. int main (void){
  4. int i =0;
  5. (i++)=100;
  6. cout << i << endl;
  7. cout << i++++++<< endl;// 103
  8. return0;
  9. }
 

array.cpp
  1. #include<iostream>
  2. usingnamespace std;
  3. classArray{
  4. public:
  5. Array(size_t size =1):
  6. m_data (newint[size]){}
  7. ~Array(void){
  8. if(m_data){
  9. delete m_data;
  10. m_data = NULL;
  11. }
  12. }
  13. int&operator[](size_t i){
  14. return m_data[i];
  15. }
  16. constint&operator[](size_t i)const{
  17. returnconst_cast<Array&>(*this)[i];
  18. }
  19. private:
  20. int* m_data;
  21. };
  22. int main (void){
  23. Array arr (10);
  24. for(size_t i =0; i <10;++i)
  25. arr[i]= i;// arr.operator[](i) = i;
  26. arr[0]++;
  27. constArray& cr = arr;
  28. for(size_t i =0; i <10;++i)
  29. cout << cr[i]<<' ';
  30. cout << endl;
  31. // cr[0]++;
  32. return0;
  33. }
 
  1. constint&operator[](size_t i)const{
  2. returnconst_cast<Array&>(*this)[i];
   const常对象的下标操作,返回右值;
   右值,即具有只读属性,不能进行赋值;
 
  return const_cast<Array&> (*this)[i];
  去常,变为普通引用;
 
  1. int&operator[](size_t i){
  2. return m_data[i];
  3. }
   非常对象的下标操作,返回左值;
  

 
c.cpp
  1. #include<iostream>
  2. usingnamespace std;
  3. classComplex{
  4. public:
  5. Complex(int r =0,int i =0):
  6. m_r (r), m_i (i){}
  7. void print (void)const{
  8. cout << m_r <<'+'<< m_i <<'i'<< endl;
  9. }
  10. private:
  11. int m_r;
  12. int m_i;
  13. };
  14. int main (void){
  15. return0;
  16. }
 

complex1.cpp
  1. #include<iostream>
  2. usingnamespace std;
  3. classComplex{
  4. public:
  5. Complex(int r =0,int i =0):
  6. m_r (r), m_i (i){}
  7. void print (void)const{
  8. cout << m_r <<'+'<< m_i <<'i'<< endl;
  9. }
  10. Complex&operator+=(constComplex& r){
  11. m_r += r.m_r;
  12. m_i += r.m_i;
  13. return*this;
  14. }
  15. friendComplex&operator-=(Complex& l,
  16. constComplex& r){
  17. l.m_r -= r.m_r;
  18. l.m_i -= r.m_i;
  19. return l;
  20. }
  21. private:
  22. int m_r;
  23. int m_i;
  24. };
  25. int main (void){
  26. Complex c1 (1,2), c2 (3,4);
  27. c1 += c2;// c1.operator+= (c2)i
  28. c1.print ();// 4+6i
  29. Complex c3 (5,6);
  30. (c1 += c2)= c3;
  31. c1.print ();// 5+6i
  32. c1 -= c2;// ::operator-= (c1, c2)
  33. c1.print ();// 2+2i
  34. (c1 -= c2)= c3;
  35. c1.print ();// 5+6i;
  36. return0;
  37.  
  38. }
operator操作符重载:
 
 

complex2.cpp
  1. #include<iostream>
  2. usingnamespace std;
  3. classComplex{
  4. public:
  5. Complex(int r =0,int i =0):
  6. m_r (r), m_i (i){}
  7. void print (void)const{
  8. cout << m_r <<'+'<< m_i <<'i'<< endl;
  9. }
  10. friend ostream&operator<<(ostream& os,
  11. constComplex& r){
  12. return os << r.m_r <<'+'<< r.m_i <<'i';
  13. }
  14. friend istream&operator>>(istream& is,
  15. Complex& r){
  16. return is >> r.m_r >> r.m_i;
  17. }
  18. private:
  19. int m_r;
  20. int m_i;
  21. };
  22. int main (void){
  23. Complex c1 (1,2), c2 (3,4);
  24. cout << c1 << endl << c2 << endl;
  25. // ::operator<<(::operator<<(cout,c1).operator<<(
  26. // endl),c2).operator<<(endl);
  27. cin >> c1 >> c2;
  28. cout << c1 << endl << c2 << endl;
  29. return0;
  30. }
 

complex3.cpp
  1. #include<iostream>
  2. usingnamespace std;
  3. classComplex{
  4. public:
  5. Complex(int r =0,int i =0):
  6. m_r (r), m_i (i){}
  7. void print (void)const{
  8. cout << m_r <<'+'<< m_i <<'i'<< endl;
  9. }
  10. constComplexoperator-(void)const{
  11. returnComplex(-m_r,-m_i);
  12. }
  13. friendconstComplexoperator~(
  14. constComplex& o){
  15. returnComplex(o.m_i, o.m_r);
  16. }
  17. private:
  18. int m_r;
  19. int m_i;
  20. };
  21. int main (void){
  22. constComplex c1 (1,2);
  23. Complex c2 =-c1;// c2=c1.operator-()
  24. c2.print ();// -1+-2i
  25. Complex c3 =~c1;// c3=::operator~(c1)
  26. c3.print ();// 2+1i;
  27. return0;
  28. }
 
理解什么是operator操作符重载: 
 
 

 
complex4.cpp
  1. #include<iostream>
  2. usingnamespace std;
  3. classComplex{
  4. public:
  5. Complex(int r =0,int i =0):
  6. m_r (r), m_i (i){}
  7. void print (void)const{
  8. cout << m_r <<'+'<< m_i <<'i'<< endl;
  9. }
  10. Complex&operator++(void){
  11. ++m_r;
  12. ++m_i;
  13. return*this;
  14. }
  15. friendComplex&operator--(Complex& o){
  16. --o.m_r;
  17. --o.m_i;
  18. return o;
  19. }
  20. private:
  21. int m_r;
  22. int m_i;
  23. };
  24. int main (void){
  25. Complex c1 (1,2);
  26. Complex c2 =++c1;// c2=c1.operator++()
  27. c1.print ();// 2+3i
  28. c2.print ();// 2+3i
  29. (++c1)=Complex(10,20);
  30. c1.print ();// 10+20i;
  31. (++++++c1).print ();// 13+23i
  32. c2 =--c1;// c2=::operator--(c1)
  33. c1.print ();// 12+22i
  34. c2.print ();// 12+22i
  35. return0;
  36. }
 
深刻理解operator操作符重载:用于复数的表示:
 
 
 

complex5.cpp
  1. #include<iostream>
  2. usingnamespace std;
  3. classComplex{
  4. public:
  5. Complex(int r =0,int i =0):
  6. m_r (r), m_i (i){}
  7. void print (void)const{
  8. cout << m_r <<'+'<< m_i <<'i'<< endl;
  9. }
  10. constComplexoperator++(int){
  11. Complex old (*this);
  12. ++m_r;
  13. ++m_i;
  14. return old;
  15. }
  16. friendconstComplexoperator--(Complex& o,
  17. int){
  18. Complex old (o);
  19. --o.m_r;
  20. --o.m_i;
  21. return old;
  22. }
  23. private:
  24. int m_r;
  25. int m_i;
  26. };
  27. int main (void){
  28. Complex c1 (1,2);
  29. Complex c2 = c1++;// c2=c1.operator++(0)
  30. c1.print ();// 2+3i;
  31. c2.print ();// 1+2i;
  32. // (c1++) = c2;
  33. // c1++++++;
  34. c2 = c1--;// c2=::operator--(c1,0)
  35. c1.print ();// 1+2i
  36. c2.print ();// 2+3i
  37. return0;
  38. }vi
 
 
 operator操作符重载--
 

date.cpp
  1. // 日期运算。
  2. // 实现日期类,支持如下运算:
  3. // +/+=:增加指定的天数;
  4. // -/-=:减去指定的天数;
  5. // - :两日期相差天数。
  6. // >> :接受形如2012 1 14格式输入;
  7. // << :以形如2012-1-14的格式输出;
  8. #include<iostream>
  9. usingnamespace std;
  10. classDate{
  11. public:
  12. Date(int nYear =0,int nMonth =0,
  13. int nDay =0):
  14. m_nYear (nYear), m_nMonth (nMonth),
  15. m_nDay (nDay){}
  16. Dateoperator+(int nDays)const{
  17. returnDays2Date(Date2Days()+ nDays);
  18. }
  19. Date&operator+=(int nDays){
  20. return*this=*this+ nDays;
  21. }
  22. Dateoperator-(int nDays)const{
  23. returnDays2Date(Date2Days()- nDays);
  24. }
  25. Date&operator-=(int nDays){
  26. return*this=*this- nDays;
  27. }
  28. intoperator-(constDate& date)const{
  29. returnDate2Days()- date.Date2Days();
  30. }
  31. friend istream&operator>>(istream& is,
  32. Date& date){
  33. return is >> date.m_nYear >> date.m_nMonth
  34. >> date.m_nDay;
  35. }
  36. friend ostream&operator<<(ostream& os,
  37. constDate& date){
  38. return os << date.m_nYear <<"-"
  39. << date.m_nMonth <<"-"<< date.m_nDay;
  40. }
  41. private:
  42. intDate2Days(void)const{
  43. int nDays =(m_nYear -1)*365;
  44. for(int nYear =1; nYear < m_nYear;
  45. nYear++)
  46. if(IsLeap(nYear))
  47. nDays++;
  48. for(int nMonth =0; nMonth < m_nMonth -1;
  49. nMonth++)
  50. if(IsLeap(m_nYear))
  51. nDays += s_nDaysOfMonth[1][nMonth];
  52. else
  53. nDays += s_nDaysOfMonth[0][nMonth];
  54. nDays += m_nDay;
  55. return nDays;
  56. }
  57. DateDays2Date(int nDays)const{
  58. int nYear =1;
  59. for(;;){
  60. if(IsLeap(nYear)){
  61. if(nDays <=366)
  62. break;
  63. nDays -=366;
  64. }
  65. else{
  66. if(nDays <=365)
  67. break;
  68. nDays -=365;
  69. }
  70. nYear++;
  71. }
  72. int nMonth =1;
  73. bool bLeap =IsLeap(nYear);
  74. for(;;){
  75. if(bLeap){
  76. if(nDays <=
  77. s_nDaysOfMonth[1][nMonth-1])
  78. break;
  79. nDays-=s_nDaysOfMonth[1][nMonth-1];
  80. }
  81.  
  82. else{
  83. if(nDays <=
  84. s_nDaysOfMonth[0][nMonth-1])
  85. break;
  86. nDays-=s_nDaysOfMonth[0][nMonth-1];
  87. }
  88. nMonth++;
  89. }
  90. returnDate(nYear, nMonth, nDays);
  91. }
  92. boolIsLeap(int nYear)const{
  93. return(nYear %4==0&& nYear %100!=0)
  94. || nYear %400==0;
  95. }
  96. int m_nYear;
  97. int m_nMonth;
  98. int m_nDay;
  99. staticint s_nDaysOfMonth[][12];
  100. };
  101. intDate::s_nDaysOfMonth[][12]={
  102. {31,28,31,30,31,30,31,31,30,31,30,31},
  103. {31,29,31,30,31,30,31,31,30,31,30,31}
  104. };
  105. int main (void){
  106. cout <<"Enter a date: ";
  107. Date d1;
  108. cin >> d1;
  109. cout <<"Enter the number of days: ";
  110. int nDays;
  111. cin >> nDays;
  112. Date d2 = d1 + nDays;
  113. cout << d1 <<" + "<< nDays <<" = "<< d2
  114. << endl;
  115. Date d3 (d1);
  116. d3 += nDays;
  117. cout << d1 <<" += "<< nDays <<" -> "<< d3
  118. << endl;
  119. cout <<"Enter a date: ";
  120. cin >> d1;
  121.  
  122. cout <<"Enter the number of days: ";
  123. cin >> nDays;
  124. d2 = d1 - nDays;
  125. cout << d1 <<" - "<< nDays <<" = "<< d2
  126. << endl;
  127. d3 = d1;
  128. d3 -= nDays;
  129. cout << d1 <<" -= "<< nDays <<" -> "<< d3
  130. << endl;
  131. cout <<"Enter a date: ";
  132. cin >> d1;
  133. cout <<"Enter another date: ";
  134. cin >> d2;
  135. cout <<"The days between those dates is "
  136. << d2 - d1 << endl;
  137. return0;
  138. }

integer.cpp
  1. #include<iostream>
  2. usingnamespace std;
  3. classSquare{
  4. public:
  5. doubleoperator()(double x){
  6. return x * x;
  7. }
  8. };
  9. classInteger{
  10. public:
  11. explicitInteger(int i =0): m_i (i){}
  12. void print (void)const{
  13. cout << m_i << endl;
  14. }
  15. Integer&operator()(int i){
  16. m_i += i;
  17. return*this;
  18. }
  19. Integer&operator,(int i){
  20. m_i += i;
  21. return*this;
  22. }
  23. operatorint(void)const{
  24. return m_i;
  25. }
  26. private:
  27. int m_i;
  28. };
  29. void foo (constInteger& i){
  30. i.print ();
  31. }
  32. Integer bar (void){
  33. returnInteger(300);
  34. }
  35. int main (void){
  36. Square square;
  37. cout << square (3)<< endl;
  38. // cout << square.operator() (3) << endl;
  39. Integer i (10);
  40. i (1)(2)(3)(17);
  41. i.print ();// 33
  42. i,1,2,3,17;
  43. i.print ();// 56
  44. i =(Integer)100;
  45. i.print ();
  46. foo (static_cast<Integer>(200));
  47. bar ().print ();
  48. int n = i;
  49. cout << n << endl;
  50. return0;
  51. }
 通过explicit关键字,可以强制使用该构造函数所完成的类型转换必须显示进行。
 

m33.cpp
  1. #include<iomanip>
  2. #include<iostream>
  3. usingnamespace std;
  4. class M33 {
  5. public:
  6. M33 (void){
  7. for(int i =0; i <3; i++)
  8. for(int j =0; j <3; j++)
  9. m_a[i][j]=0;
  10. }
  11. M33 (int a[][3]){
  12. for(int i =0; i <3; i++)
  13. for(int j =0; j <3; j++)
  14. m_a[i][j]= a[i][j];
  15. }
  16. const M33 operator+(const M33& m)const{
  17. int a[3][3];
  18. for(int i =0; i <3; i++)
  19. for(int j =0; j <3; j++)
  20. a[i][j]= m_a[i][j]+ m.m_a[i][j];
  21. return a;
  22. }
  23. const M33 operator-(const M33& m)const{
  24. int a[3][3];
  25. for(int i =0; i <3; i++)
  26. for(int j =0; j <3; j++)
  27. a[i][j]= m_a[i][j]- m.m_a[i][j];
  28. return a;
  29. }
  30. const M33 operator*(const M33& m)const{
  31. int a[3][3]={0};
  32. for(int i =0; i <3; i++)
  33. for(int j =0; j <3; j++)
  34. for(int k =0; k <3; k++)
  35. a[i][j]+=m_a[i][k]*m.m_a[k][j];
  36. return a;
  37. }
  38. M33&operator+=(const M33& m){
  39. return*this=*this+ m;
  40. }
  41. M33&operator-=(const M33& m){
  42. return*this=*this- m;
  43. }
  44. M33&operator*=(const M33& m){
  45. return*this=*this* m;
  46. }
  47. const M33 operator-(void)const{
  48. return M33 ()-*this;
  49. }
  50. M33&operator++(void){
  51. for(int i =0; i <3; i++)
  52. for(int j =0; j <3; j++)
  53. m_a[i][j]++;
  54. return*this;
  55. }
  56. M33&operator--(void){
  57. for(int i =0; i <3; i++)
  58. for(int j =0; j <3; j++)
  59. m_a[i][j]--;
  60. return*this;
  61. }
  62. const M33 operator++(int){
  63. M33 m =*this;
  64. ++(*this);
  65. return m;
  66. }
  67. const M33 operator--(int){
  68. M33 m =*this;
  69. --(*this);
  70. return m;
  71. }
  72. friend ostream&operator<<(ostream& os,
  73. const M33& m){
  74. for(int i =0; i <3; i++){
  75. for(int j =0; j <3; j++)
  76. os << setw (4)<< m.m_a[i][j];
  77. os << endl;
  78. }
  79. return os;
  80. }
  81. private:
  82. int m_a[3][3];
  83. };
  84. int main (void){
  85. int a1[3][3]={1,2,3,4,5,6,7,8,9};
  86. M33 m1 (a1);
  87. int a2[3][3]={9,8,7,6,5,4,3,2,1};
  88. M33 m2 (a2);
  89. cout << m1 + m2 << endl;
  90. cout << m1 - m2 << endl;
  91. cout << m1 * m2 << endl;
  92. m1 += m2;
  93. cout << m1 << endl;
  94. m1 -= m2;
  95. cout << m1 << endl;
  96. m1 *= m2;
  97. cout << m1 << endl;
  98. cout <<-m1 << endl;
  99. cout <<++m2 << endl;
  100. cout << m2 << endl;
  101. cout <<--m2 << endl;
  102. cout << m2 << endl;
  103. cout << m2++<< endl;
  104. cout << m2 << endl;
  105. cout << m2--<< endl;
  106. cout << m2 << endl;
  107. return0;
  108. }
 
 
 
 
 cout << m1 + m2 << endl; 
 
 
 (1) 常对象
用const修饰的对象叫对象常量,其格式如下:
〈类名〉const 〈对象名〉 或者 const 〈类名〉〈对象名〉
声明为常对象的同时必须被初始化,并从此不能改写对象的数据成员。
 const M33 operator+(const M33& m)const
 
常对象常被用在对象的引用上。所谓常引用是指说明引用时用const修饰
常引用所引用的对象不能被更新,一般用做形参。其格式:
const 〈类型说明〉&〈引用名〉 
const M33 operator+(const M33& m)const
 
(2)常成员函数 用const关键词说明的函数叫常成员函数。其格式如下:
〈类型〉〈函数名〉(〈参数表〉)const;
const M33 operator+(const M33& m)const
 
 

new.cpp
  1. #include<iostream>
  2. #include<cstdlib>
  3. usingnamespace std;
  4. class A {
  5. public:
  6. ~A (void){}
  7. staticvoid*operatornew(size_t size){
  8. void* p = malloc (size);
  9. cout <<"我的new :"<< p <<' '
  10. << size << endl;
  11. return p;
  12. }
  13. staticvoidoperatordelete(void* p){
  14. cout <<"我的delete:"<< p << endl;
  15. free (p);
  16. }
  17. staticvoid*operatornew[](size_t size){
  18. void* p = malloc (size);
  19. cout <<"我的new[] :"<< p <<' '
  20. << size << endl;
  21. return p;
  22. }
  23. staticvoidoperatordelete[](void* p){
  24. cout <<"我的delete[]:"<< p << endl;
  25. free (p);
  26. }
  27. private:
  28. int m_i;
  29. double m_d;
  30. char m_c;
  31. };// IIIIDDDDDDDDCXXX
  32. int main (void){
  33. cout <<sizeof(A)<< endl;
  34. A* pa =new A;
  35. cout << pa << endl;
  36. delete pa;
  37. pa =new A[2];
  38. cout << pa << endl;
  39. delete[] pa;
  40. return0;
  41. }
 
到下列东西时要用
  #include <cstdlib>
  字符转换函数:atof,atoi,atol,strtod,strtol,strtoul 
  伪随机数函数:rand,srand
  动态分配内存函数:calloc,free,malloc,realloc
  环境函数:abort,atexit,exit,getenv,system 
  查找,分类函数:bsearch,qsort 
  整数计算函数:abs,div,labs,ldiv,
  多字节文字(中日韩文)函数:mblen,mbtowc,wctombmbstowcs,wcstombs,     宏:EXIT_FAILURE,EXIT_SUCCESS,MB_CUR_MAX,NULL,RAND_MAX,  类型:div_t,ldiv_t,size_t
 delete      pa;   局部释放
 delete[ ]   pa;    全部释放 
 

ptr.cpp
  1. #include<iostream>
  2. #include<memory>
  3. usingnamespace std;
  4. class A {
  5. public:
  6. A (void){
  7. cout <<"构造"<< endl;
  8. }
  9. ~A (void){
  10. cout <<"析构"<< endl;
  11. }
  12. void hello (void){
  13. cout <<"Hello, World !"<< endl;
  14. }
  15. };
  16. class PA {
  17. public:
  18. PA (A* p = NULL): m_p (p){}
  19. ~PA (void){
  20. if(m_p){
  21. delete m_p;
  22. m_p = NULL;
  23. }
  24. }
  25. A&operator*(void)const{
  26. return*m_p;
  27. }
  28. A*operator->(void)const{
  29. // return &**this;
  30. return m_p;
  31. }
  32. PA (PA& that): m_p (that.release ()){}
  33. PA&operator=(PA& that){
  34. if(&that !=this)
  35. reset (that.release ());
  36. return*this;
  37. }
  38. private:
  39. A* release (void){
  40. A* p = m_p;
  41. m_p = NULL;
  42. return p;
  43. }
  44. void reset (A* p){
  45. if(p != m_p){
  46. delete m_p;
  47. m_p = p;
  48. }
  49. }
  50. A* m_p;
  51. };
  52. void bar (auto_ptr<A> pa){
  53. cout <<"bar:";
  54. pa -> hello ();
  55. }
  56. void foo (void){
  57. PA pa (new A);
  58. // A* pa = new A;
  59. // pa -> hello ();
  60. // (*pa).hello ();
  61. // A* pb = pa;
  62. pa -> hello ();// pa.operator->()->hello();
  63. (*pa).hello ();// pa.operator*().hello();
  64. PA pb = pa;
  65. pb -> hello ();
  66. auto_ptr<A> pa1 (new A);
  67. pa1 -> hello ();
  68. auto_ptr<A> pa2 = pa1;
  69. (*pa2).hello ();
  70. bar (pa2);
  71. cout << pa2.get ()<< endl;
  72. }
  73. // smart_ptr
  74. int main (void){
  75. foo ();
  76. return0;
  77. }
 
 若在栈里创建,找到右括号}会析构;
 若new 是在堆里,不会自动析构;
  PA pa = new A;   
  在堆里new,析构交给智能指针PA;
--------------------------------------------------------
  智能指针:把类类型的对象当作指针来用;    // pa 是对象
  我们希望用指针的形式去用一个类型的对象;
 
 拷贝构造:把PA中对象的指针一个个进行传递;
 进行拷贝赋值;
 先把原来持有的去掉,持新的指针。
 
智能指针,任何时候都仅持有一个指针。
 
foo 是成员变量
foobar 是全局变量
barfoo 是 Foo::bar() 的局部变量
bar 的作用是根据某个全局状态和之前计算出来的值修改自身的某个状态
 
 (1)不能跨作用域;
 (2)谨慎复制;
 (3)不能使用数组;
 

string.cpp
  1. #include<iostream>
  2. #include<cstring>
  3. usingnamespace std;
  4. classString{
  5. public:
  6. String(constchar* str = NULL){
  7. m_str =newchar[strlen(str?str:"")+1];
  8. strcpy (m_str, str ? str :"");
  9. }
  10. ~String(void){
  11. if(m_str){
  12. delete[] m_str;
  13. m_str = NULL;
  14. }
  15. }
  16. String(constString& that):
  17. m_str (strcpy (
  18. newchar[strlen(that.m_str)+1],
  19. that.m_str)){}
  20. /* 菜鸟
  21. void operator= (const String& that) {
  22. m_str = new char[strlen(that.m_str)+1];
  23. strcpy (m_str, that.m_str);
  24. }*/
  25. String&operator=(constString& that){
  26. if(&that !=this){
  27. /* 小鸟
  28. delete[] m_str;
  29. m_str = new char[strlen(that.m_str)+1];
  30. strcpy (m_str, that.m_str);
  31. */
  32. /* 大鸟
  33. char* str =
  34. new char[strlen(that.m_str)+1];
  35. delete[] m_str;
  36. m_str = strcpy (str, that.m_str);
  37. */
  38. // 老鸟
  39. String temp (that);
  40. swap (m_str, temp.m_str);
  41. }
  42. return*this;
  43. }
  44. constchar* c_str (void)const{
  45. return m_str;
  46. }
  47. operatorconstchar*(void)const{
  48. return c_str ();
  49. }
  50. private:
  51. char* m_str;
  52. };
  53. int main (void){
  54. String s1 ("Hello, World !");
  55. cout << s1.c_str ()<< endl;
  56. String s2 = s1;
  57. cout << s2.c_str ()<< endl;
  58. String s3 ("Hello, Linux !");
  59. try{
  60. s1 = s3;
  61. }
  62. catch(exception& ex){
  63. cout << ex.what ()<< endl;
  64. }
  65. cout << s1.c_str ()<< endl;
  66. constchar* psz = u1;
  67. cout << s1 << endl;
  68. cout << psz << endl;
  69. return0;
  70. }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">





posted on 2018-11-29 23:00  XuCodeX  阅读(194)  评论(0编辑  收藏  举报

导航