读书人

单步通过执行通不过?该怎么解决

发布时间: 2012-03-08 13:30:13 作者: rapoo

单步通过,执行通不过???
我写了一个程序,是The C++ programming language 里的一道习题。我在vc2005里编译,运行时出错,但用F10单步结果却是正确的!!!这样我就无论如何也看不出我的程序错在什么地方了。我把代码放到vc6中编译执行,一切正常!!这是教材十一章运算符重载的习题,很多代码都是照抄书上的,但是重载+号函数是我自己写的,估计问题在这里。有没有高手帮我看看啊?程序比较长...
#include <iostream>
#include <string>
using namespace std;
class String
{
struct Srep;
Srep* rep;
public:
class Cref;
class Range{};

String();
String(const char*);
String(const String&);
String& operator=(const String&);
String& operator=(const char*);
~String();

void check(int i) const;
char read(int i) const;
void write(int i,char c);
Cref operator[](int i);
char operator[](int i) const;
int size()const;
char* c_str()const;

String& operator+=(const String&);



};
String operator+(const String& x,const String& y)
{
char* t=new char[x.size()+y.size()+1];
t=strcat(x.c_str(),y.c_str());
cout < < "ready to leave opeartor+ \n ";
//return String(t);
cout < <t < <endl;

return String(t);
}

class String::Cref
{
friend class String;
String& s;
int i;
Cref(String& ss,int ii):s(ss),i(ii){};
public:
operator char() const{return s.read(i);}
void operator=(char c) {s.write(i,c);}
};
struct String::Srep
{
char* s;
int sz;
int n;
Srep(int nsz,const char* p)
{
cout < < "now in Srep construct\n ";
n=1;
sz=nsz;
s=new char[sz+1];
strcpy(s,p);
cout < < "leaving srep construct\n ";
}
~Srep(){ delete[] s;}
Srep* get_own_copy()
{
if(n==1) return this;
n--;
return new Srep(sz,s);
}
void assign(int nsz,const char* p)
{
if(sz!=nsz)
{
delete[] s;
sz=nsz;
s=new char[sz+1];
}
strcpy(s,p);
}
private:
Srep(const Srep&);
Srep& operator=(const Srep&);
};

String::String()
{
rep=new Srep(0, " ");
}
String::String(const String& x)
{
cout < < "now in copy construct " < <endl;
x.rep-> n++;
rep=x.rep;
cout < < "now leaving copy construct " < <endl;
}
String::String(const char* s)
{
cout < < "come into construct with const char* " < <endl;
rep=new Srep(strlen(s),s);
cout < < "Leaving construct with const char* " < <endl;
}
String& String::operator=(const String& x)
{
x.rep-> n++;
if(--rep-> n==0) delete rep;
rep=x.rep;
return *this;
}
String& String::operator=(const char* s)
{
if(rep-> n==1)


rep-> assign(strlen(s),s);
else
{
rep-> n--;
rep=new Srep(strlen(s),s);
}
return *this;
}
String::~String()
{
if(--rep-> n==0) delete rep;
}


void String::check(int i) const
{
if(i <0||rep-> sz <=i)throw Range();
}
char String::read(int i) const
{return rep-> s[i];}
void String::write(int i,char c)
{
rep=rep-> get_own_copy();
rep-> s[i]=c;
}
String::Cref String::operator[](int i)
{
check(i);
return Cref(*this,i);
}
char String::operator[](int i) const
{
check(i);
return rep-> s[i];
}
int String::size()const
{
return rep-> sz;
}

char* String::c_str()const
{
char* t=new char[rep-> sz+1];
strcpy(t,rep-> s);
return t;
}
String& String::operator+=(const String& s)
{
char* t=s.c_str();
char* p=new char[strlen(rep-> s)+strlen(t)+1];
strcpy(p,rep-> s);
strcpy(p+strlen(rep-> s),t);
delete[] t;
*this=p;
delete[] p;
return *this;
}

void main()
{
String a= "Hex ";
String b= "yinzixinhelloworld ";
cout < <a.size() < < " " < <b.size() < <endl;
String c;
c=a+b;
cout < <c.size() < <endl;
cout < <c.c_str();
}

[解决办法]
char* String::c_str()const
{
char* t=new char[rep-> sz+1]; //还有这里new的内存. 用完了也没有delete. 这也不好
strcpy(t,rep-> s);
return t;
}

读书人网 >C++

热点推荐