📄 ex17.cpp
字号:
{ double **pp; //动态开辟的二维数组首地址
int m_nRow; // m_nRow 为行的维数
int m_nColumn; //m_nColumn为列的维数
public : CType(int r,int c=1); //设置一个默认值的构造函数
~CType();
double& operator()(int r,int c=0); //设置一个默认值
};
CType::CType(int row,int column) //构造函数动态申请内存
{ m_nRow=row;
m_nColumn= column; //首先申请row个行的数组指针
pp=new double*[row]; //相当于double* pp [m_nRow];
for(int j=0;j< row;j++) //每一个指针元素进一步要求内存
pp[j]=new double[column]; //相当于double pp[j] [column];
}
inline double& CType::operator()(int r,int c) { return pp[r][c]; }
CType::~CType()
{ for(int j=0;j< m_nRow;j++) //析构函数反序释放内存
delete [] pp[j]; //先清理指针元素分管的内存
delete [] pp; //再撤销二级指针总管的内存
}
#include<stdio.h>
void main() //类似于FORTRAN数组的定义格式
{ CType a(2,3); //定义二维数组a
CType b(3); //等价于CType b(3,1); 定义一个类似一维数组的b
a(0,0)=1;a(0,1)=2;a(0,2)=3; //以FORTRAN圆括号形式使用数组元素
a(1,0)=2;a(1,1)=4;
a.operator ()(1,2)=6; //显式执行函数调用运算符函数
b(0)=2;b(1)=2;b(2)=2; //等价于b(0,0)=2;b(1,0)=2;b(2,0)=2;
printf("a(0)*b=%f\t",a(0,0)*b(0)+a(0,1)*b(1)+a(0,2)*b(2));
printf("a(1)*b=%f\n",a(1,0)*b(0)+a(1,1)*b(1)+a(1,2)*b(2));
} //输出:a(0)*b=12.000000 a(1)*b=24.000000
/// [例17.8] smart指针other涉及的主要步骤
#include<stdio.h>
class CType //声明一个直接类CType
{ public: //简单地设置一个公共的成员
int m_right;
} obj={1}; //定义全局对象obj并设置初始值
class CTypeOther //声明一个间接类CTypeOther
{ CType* ptrExpre; //间接类一个重要的CType*指针成员
public: CTypeOther(CType* pobj) { ptrExpre=pobj;}//公共的构造函数
CType* operator->() { return ptrExpre ;} //访问成员运算符函数
};
void main()
{ //定义间接类的对象other
CTypeOther other(&obj); //调用构造函数CTypeOther(CType*)
other.operator->()->m_right+=1000; //直接调用访问成员运算符函数
printf("%d\t",other->m_right); // other->m_right遥控其它类的数据
CTypeOther * pO=&other; //定义一个CTypeOther * 对象指针pO
(*pO)->m_right+=1000; //对象指针pO访问obj对象的数据
pO->operator ->(); //对象指针访问成员函数
pO->operator ->()->m_right+=1000; //返回指针的函数接力运算
printf("%d\n", (&obj) ->m_right);
} //输出结果:1001 3001
/////[例17.9]字符串类的下标运算符函数
#include <stdio.h>
#include<string.h>
char * pch; //!!定义一个侵略性的全局指针
class CType
{ public:
CType(const char* s) ;
CType& operator=(const CType& r); //等号运算符函数operator=
operator const char*() const{ return m_pData;} //只读类型转换运算符函数
operator char*() { return m_pData;} //类型转换运算符函数
const char& operator[] (int n) const ; //只读下标运算符函数
char& operator[] (int n) ; //下标运算符函数operator[]
~CType() { delete [] m_pData; }
private:
char * m_pData; //一个指针成员用于定位动态数组
int m_nLength; //描述动态数组的动态维数
static char cErro; //声明一个静态成员变量
};
char CType::cErro=-1; //全局范围定义静态成员变量
const char& CType::operator[] (int n) const //只读下标运算符函数定义
{ if(n>=0&&n<m_nLength) return m_pData[n];
else { printf("\nconst!bound violated!\t"); return cErro; }
}
char& CType::operator[] (int n) //下标运算符函数定义
{ if(n>=0&&n<m_nLength) //维数越界检查
return m_pData[n]; //不越界则返回数组元素
else { printf("bound violated!\n"); //越界时显示出错信息
return cErro;} //出错时返回一个静态的成员变量
}
CType::CType(const char* s) //单参数入口的构造函数
{ m_nLength=strlen(s);
m_pData=new char[m_nLength+1]; //分配动态数组的内存空间
strcpy(m_pData,s); //全盘拷贝入口字符串数据到深部动态内存
pch=m_pData; //!!全局指针在构造函数中赋值
}
CType& CType::operator=(const CType& r) //等号运算符函数
{ if(m_nLength!=r.m_nLength) //如目标串和源串长度不一,则重新内存配置
{ delete [] m_pData; //释放原来的内存空间
m_nLength=r.m_nLength; //设置新的内存空间大小
m_pData=new char[m_nLength+1]; //申请内存空间
}
strcpy(m_pData,r.m_pData); //字符串数据的深拷贝
return *this; //返回已经刷新的当前对象
}
inline void display(const char* s){ printf("%s\t",s);}
void main()
{ const CType abc("ABCDEFGH"); //定义只读对象abc ,此时pch=abc.m_pData;
display(abc); //调用只读类型转换函数
pch[0]='a';pch[1]='b';pch[2]='c'; //全局指针侵犯只读对象区
const char cs[]="12345"; //定义只读数组
pch=(char*)cs+1; //全局指针指向只读数组cs
*pch='*'; //全局指针侵犯只读数组区
CType num=cs; //定义对象num,此时pch=num.m_pData;
display(num); //调用operator char*()
const int len=strlen(num); // strlen(num)调用operator char*()
int k ;for(k=0;k<len;k++) num[k]=abc[k];//调用两个下标运算符函数
display(num);
num[k]=abc[k+len]; //调用两个下标运算符函数,同时越界
num=abc; //调用等号运算符函数
display(num); //程序运行输出结果如下:
for(k=0;k<len;k++) pch[k]=cs[k]; // ABCDEFGH 1*345 abcDE
display(num); // const!bound violated! bound violated!
} //abcDEFGH 1*345FGH
//[例17.10]重载需要左值作为操作数的运算符以支撑枚举变量的相关运算
#include<stdio.h>
enum EName { E1=1,E2, E3,E4,E5,E6,E7 };
const EName operator++(EName& e,int) //重载后置自增运算符L++
{ const EName temp=e; e=(EName) (temp+1);
return temp;
}
inline EName& operator+=(EName& e,int x) { return e=(EName) (e+x); }
inline EName& operator-=(EName& e,int x) { return e=(EName) (e-x); }
void main()
{ EName e1=E1, e2=(EName)1; e2+=E6; e1++;
if(e1==E2) printf("e1==E2=%d\t",e1);
e2-=E6; //++e1;error 'enum EName' does not define this operator
if(e2==E1) printf("e2==E1=%d\n",e2);
} //输出结果: e1==E2=2 e2==E1=1
267
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -