返回的时候,如果局部变量是一个指针,返回的是局部变量的地址是会出错的。
因为返回的时候,这个局部变量已经消亡了,返回地址会是一片未知地址,造成麻烦。
解决办法:在函数中的局部变量前面加上static,成为静态变量。
具体例子:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int MAX = 110;
class CHugeInt {
int number[201];
public:
CHugeInt(int n) {
memset(number, 0, sizeof(number));
int pos = 200;
while ((n / 10) || (n % 10)) {
number[pos] = n % 10;
n /= 10;
pos--;
}
//cout << *this;
}
CHugeInt(const char* w) {
memset(number, 0, sizeof(number));
int pos = 200;
int posw = strlen(w) - 1;
for (int i = posw; i >= 0; i--) {
number[pos] = w[i] - '0';
pos--;
}
//cout << *this;
}
CHugeInt(const CHugeInt &a) {
for (int i = 200; i >= 0; i--) {
number[i] = a.number[i];
}
}
friend CHugeInt& operator+(