Istream 运算符重载'>>'导致无限循环

Istream operator overloading '>>' cause infinite loop

本文关键字:gt 无限循环 运算符 重载 Istream      更新时间:2023-10-16

我的大学老师布置了关于模拟字符串的家庭作业,这是我的一个代码选择:

// 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 =在这里导致无限循环......请帮忙...提前致谢

编辑: 有我的错误代码。 我已经编辑了导致无限循环问题的原因,但其余的可能仍然有错误,我会尽快修复它们,谢谢大家!

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;
}

我的字符串.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。当本地数组被销毁时,函数结束时会发生什么?指针变为无效。

您需要实现深层复制。