采用了ABC模型和多态
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class BaseExcepyion
{
public:
virtual void what()=0;
virtual ~BaseExcepyion(){}
private:
};
class Tnull:public BaseExcepyion
{
public:
void what()
{
cout << "目标字符串为空" << endl;
return;
}
virtual ~Tnull(){}
private:
};
class Snull:public BaseExcepyion
{
public:
void what()
{
cout << "源字符串为空" << endl;
return;
}
virtual ~Snull(){}
private:
};
void test(char *target, char *source)
{
if (target==NULL)
{
throw Tnull();
}
if (source==NULL)
{
throw Snull();
}
//一种copy的方式
/*while (*target!='\0')
{
*source = *target;
source++;
target++;
}*/
int len = strlen(target);
for (int i = 0; i < len; i++)
{
source[i] = target[i];
}
}
int main(void)
{
char *str = "";
char buf[1024] = { 0 };
try
{
test(NULL, buf);//没有目标字符串 看看异常是否正确
}
catch (BaseExcepyion &e)
{
e.what();
}
cout << buf << endl;
system("pause");
return 0;
}