超级简单的问题。。。非常感谢。。。如果你帮忙看看。。。。。
#include <iostream>
#include <cstring>
using namespace std;
class StringBad
{
private:
int len;
char *str;
static int num_strings;
public:
StringBad(const char *s);
StringBad();
~StringBad();
friend ostream & operator << (ostream &os,const StringBad &st);
};
int StringBad::num_strings=0;
StringBad::StringBad(const char *s)
{
len=strlen(str);
s=new char[len+1];
strcpy(str,s);
num_strings++;
cout<<num_strings<<" \" "<<str <<" \" object created!";
}
StringBad::StringBad()
{
len=4;
str=new char[4];
strcpy(str,"C++");
num_strings++;
cout<<num_strings<<" \" "<<str<<" \" object created!";
}
StringBad::~StringBad()
{
cout<<"\" " <<str<<" \" object deleted!";
--num_strings;
cout<<num_strings<<" left \n";
delete [] str;
}
ostream &operator<<(ostream &os,const StringBad &st)
{
os<<st.str;
return os;
}
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
void callme1(StringBad &);
void callme2(StringBad);
int main()
{
StringBad headline1("Celery Stalks at Midnight");
StringBad headline2("Lettuce Prey");
StringBad sports("Spinach Levaes Bow1 for Dollars");
cout<<"headline1: " << headline1 <<endl;
cout<<"headline2: " << headline2 <<endl;
cout<<"sports:"<<sports<<endl;
callme1(headline1);
cout<<"headline1: " <<headline1<<endl;
callme2(headline2);
cout<<"headline2: " <<headline2<<endl;
cout<<"初始化另一个对象:\n";
StringBad sailor=sports;
cout<<"sailor:" <<sailor<<endl;
cout<<"Assign one object to another: \n";
StringBad knot;
knot=headline1;
cout<<"knot:" <<knot<<endl;
cout<<"Enf of main() \n";
return 0;
}
void callme1(StringBad &s)
{
cout << "String passed by reference : \n";
cout << " \"" <<s <<" \" \n";
}
void callme2(StringBad sb)
{
cout<<"String passed by value : \n";
cout <<" \" "<<sb<<" \" \n";
}
======================================================
以上代码用7.1 CL编译通过。但会出现异常。。。
请帮忙看看。