我的大学老师分配了关于模拟字符串的作业,这是我选择的代码之一:
// MyString.h
#include <iostream>
#include <cstdio>
#include <string.h>
using std::ostream;
using std::istream;
class MyString{
protected:
char *_data; // data index
int _len; // index size
public:
/////////////////////////////////////////////////////////
char* data()const{
return _data;
}
/////////////////////////////////////////////////////////
MyString& operator=(const char* &rhs){
_data = const_cast<char*>(rhs);
_len = strlen(_data)
}
MyString& operator=(const MyString& rhs){
(*this) = rhs._data;
}
/////////////////////////////////////////////////////////
friend ostream &operator<<( ostream&, const MyString & );
friend istream &operator>>( istream&, MyString & );
};
ostream& operator <<(ostream& os, const MyString& str){
os << str.data();
return os;
}
istream& operator >>(istream& is, MyString& str){
char buffer[0xff];
is.getline(buffer, 0xff);
str = buffer;
return is;
}
我完全不知道为什么
operator =
在这里导致无限循环...请帮助...在此先感谢编辑:
我的 buggy 代码到了。我已经编辑了导致无限循环问题的原因,但其他问题可能仍然存在问题,我将尽力尽快修复它们,谢谢大家!
MyString.h
#include <iostream>
#include <cstdio>
#include <string.h>
using std::ostream;
using std::istream;
class MyString{
protected:
char *_data; // data index
int _len; // index length
static int _total_MyString; // number of instance created
public:
/////////////////////////////////////////////////////////
// basic initialization
MyString(){
_data = NULL;
_len = 0;
_total_MyString++;
}
// init with defined string
MyString(const char *s):MyString(){
(*this)= s;
}
// init with same class data
MyString(const MyString & s):MyString(s._data){}
// finalizer
~MyString(){
_total_MyString--;
delete _data;
}
/////////////////////////////////////////////////////////
static int total_MyString(){
return _total_MyString;
}
/////////////////////////////////////////////////////////
char* data()const{
return _data;
}
/////////////////////////////////////////////////////////
MyString& operator=(const char* &rhs){
char* temp = const_cast<char*>(rhs);
_len = strlen(temp);
_data = new char [_len + 1];
strcpy(_data, rhs);
return (*this);
}
MyString& operator=(const MyString& rhs){
const char* temp = rhs._data;
return (*this = temp);
}
/////////////////////////////////////////////////////////
MyString& operator+=(const char* &rhs){
_len += strlen(rhs);
strcat(_data, rhs);
return *this;
}
MyString& operator+=(const MyString& rhs){
const char* temp = rhs._data;
(*this) += temp;
return *this;
}
/////////////////////////////////////////////////////////
inline char & operator[](const int pos){
return _data[pos];
}
/////////////////////////////////////////////////////////
unsigned length() const{
return _len;
}
/////////////////////////////////////////////////////////
friend ostream &operator<<( ostream&, const MyString & );
friend istream &operator>>( istream&, MyString & );
/////////////////////////////////////////////////////////
bool operator==(const MyString& str){
return !strcmp(_data, str._data);
}
friend bool operator!=(const MyString& str, const MyString& str2){
return strcmp(str._data, str2._data) != 0;
}
};
ostream& operator <<(ostream& os, const MyString& str){
os << str.data();
return os;
}
istream& operator >>(istream& is, MyString& str){
char buffer[0xff];
is.getline(buffer, 0xff);
const char* temp = &buffer[0];
str = temp;
return is;
}
MyString.cpp
#include "MyString.h"
using namespace std ;
int MyString::_total_MyString = 0;
int main()
{
MyString Dstr1("String"), Dstr2("Test String 2"), Dstr3(Dstr1);
cout << "Dstr1 is: " << Dstr1 << endl;
cout << "Dstr2 is: " << Dstr2 << endl;
cout << "Dstr3 is: " << Dstr3 << endl;
cout << "Total MyString_Derived is: " << MyString_Derived::total_MyString() << endl;
/////////////////////////////////////////////////////////
cout << "Give one word "<< endl;
cin >> Dstr1;
cout << "Dstr1 is: " << Dstr1 << endl;
/////////////////////////////////////////////////////////
Dstr2 = Dstr1;
cout << "Dstr2 is: " << Dstr2 << endl;
return 0;
/////////////////////////////////////////////////////////
Dstr3 += Dstr1;
cout << "Dstr3 is: " << Dstr3 << endl;
/////////////////////////////////////////////////////////
cout << "Dstr3 is: ";
for(int i = 0; i<Dstr3.length(); i++)
cout << Dstr3[i];
cout << endl;
for(int i = 0; i<Dstr3.length(); i++)
Dstr3[i] = 'a' + i;
cout << "Dstr3 is: " << Dstr3 << endl;
/////////////////////////////////////////////////////////
cout << "Dstr1 is: " << Dstr1 << endl;
cout << "Dstr2 is: " << Dstr2 << endl;
cout << "Dstr3 is: " << Dstr3 << endl;
cout << "Compare Dstr1 == Dstr2 is: " << (Dstr1 == Dstr2) << endl;
cout << "Compare Dstr1 == Dstr3 is: " << (Dstr1 == Dstr3) << endl;
cout << "Compare Dstr1 != Dstr2 is: " << (Dstr1 != Dstr2) << endl;
cout << "Compare Dstr1 != Dstr3 is: " << (Dstr1 != Dstr3) << endl;
}
最佳答案
考虑operator>>
istream& operator >>(istream& is, MyString& str){
char buffer[0xff];
is.getline(buffer, 0xff);
const char* temp = &buffer[0];
str = temp;
return is;
}
buffer
是局部变量。您将创建一个指向此本地数组的指针,并将其关联到str
。那叫MyString& operator=(const char* &rhs){
_data = const_cast<char*>(rhs);
_len = strlen(_data);
return (*this);
}
有趣的是:作为参数,您将对指针的引用称为
const
,然后从中删除常量性并将其分配给_data
。现在
char* _data
指向本地数组buffer
。当本地数组被破坏时,在函数末尾会发生什么?指针变为无效。您需要实现一个深拷贝。