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

时间:2018-05-30 06:10:34

标签: c++

我的大学老师分配了一个关于模拟字符串的作业,这里有一个我的代码选择:

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

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

1 个答案:

答案 0 :(得分:0)

考虑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。当本地数组被销毁时,函数结尾会发生什么?指针变为无效。

您需要实施深层复制。