重载字符串的插入和提取操作符

时间:2012-04-30 12:55:54

标签: c++ string operator-overloading ostream insertion

我想实现这两个函数,以便我能够输入和输出我的类的对象。我得到了>>运营商工作但不是那么多<<。

这是我的.h文件中的代码:

    class MyString
    {
        public:
            MyString();
            MyString(char *message);
            ~MyString();
            void Print();



            void Copy(MyString& rhs);
            int Length();

            MyString& operator()(const int index, const char b);
            char& operator()(const int i);
            MyString& operator=(const MyString& rhs);
            bool operator==(const MyString& other) const;
            bool operator!=(const MyString& other) const;
            const MyString operator+(const MyString& rhs) const;

            MyString& operator+=(const MyString& rhs);

       private:

            char *String;
            int Size;
    };

    istream& operator>>(istream& input, MyString& rhs);
    ostream& operator<<(ostream& output, const MyString& rhs);

以下是我的.cpp文件中两个函数的代码:

 MyString::MyString()
{
    char temp[] = "Hello World";

    int counter(0);
    while(temp[counter] != '\0') {
            counter++;
    }
    Size = counter;
    String = new char [Size];
    for(int i=0; i < Size; i++)
            String[i] = temp[i];

}
MyString::MyString(char *message)
 {

    int counter(0);

    while(message[counter] != '\0') {
            counter++;
    }
    Size = counter;
    String = new char [Size];
    for(int i=0; i < Size; i++)
            String[i] = message[i];

}

  MyString::~MyString()
  {
    delete [] String;
  }
 int MyString::Length()
  {
    int counter(0);

    while(String[counter] != '\0')
    {
            counter ++;
    }





    return (counter);
  }

const MyString MyString::operator+(const MyString& rhs) const
 {

    char* tmp = new char[Size + rhs.Size +1];

    for(int i = 0; i < Size; i++)
    {
            tmp[i] = String[i];
    }
    for(int i = 0; i < rhs.Size+1; i++)
    {
             tmp[i+Size] = rhs.String[i];
   }

const MyString MyString::operator+(const MyString& rhs) const
  {

    char* tmp = new char[Size + rhs.Size +1];

    for(int i = 0; i < Size; i++)
    {
            tmp[i] = String[i];
    }
    for(int i = 0; i < rhs.Size+1; i++)
    {
            tmp[i+Size] = rhs.String[i];
    }

    MyString result;


    delete [] result.String;
    result.String = tmp;
    result.Size = Size+rhs.Size;

    return result;

  }
MyString& MyString::operator()(const int index, const char b)
 {
    if(String[index] == '\0')
    {
            exit(1);
    }
    else
    {

            String[index] = b;
    }


  }

MyString& MyString::operator=(const MyString& rhs)
 {

    if(this != &rhs)
    {

            delete [] String;
            String = new char[rhs.Size];
            Size = rhs.Size;

    for(int i = 0; i < rhs.Size+1 ; i++)
    {
            String[i] = rhs.String[i];
    }



    }

    return *this;


 }
  void MyString::Copy(MyString& rhs)
 {
    delete [] String;
    Size = rhs.Size;
    String = new char[rhs.Size];

    String = rhs.String;
  }

MyString& MyString::operator+=(const MyString& rhs)
  {

    char* tmp = new char[Size + rhs.Size + 1];

    for(int i = 0; i < Size; i++)
    {
            tmp[i] = String[i];
    }

    for(int i = 0; i <rhs.Size+1; i++)
    {
            tmp[i+Size] = rhs.String[i];
    }


    delete [] String;
    String = tmp;

    Size += rhs.Size;

    return *this;


 }

bool MyString::operator!=(const MyString& other) const
 {

    return !(*this == other);


  }
bool MyString::operator==(const MyString& other)const
  {
    if(other.Size == this->Size)
    {
            for(int i = 0; i < this->Size+1; i++)
            {
                    if(&other == this)

                            return true;
            }
    }
    else
            return false;
  }

 char& MyString::operator()(const int i)
{

    if( String[i] == '\0')
    {
            exit(1);
    }
    else
    {

    return String[i];

      }
 }



   void MyString::Print()
 {
    for(int i=0; i < Size; i++)
            cout << String[i];
    cout << endl;

  }




istream& operator>>(istream& input, MyString& rhs)
{
    char* temp;
    int size(256);
    temp = new char[size];
    input.getline(temp,size);
    rhs = MyString(temp);
    delete [] temp;

    return input;
}

ostream& operator<<(ostream& output, const MyString& rhs)
{
    char* p;
    int size(256);
    p = new char[size];
    output << rhs.MyString(p);
    delete [] p;
    return output;
}

以下是main.cpp文件中的调用方式:

cin >> SearchString >> TargetString; // Test of cascaded string-extraction operator<</*

if(SearchString.Find(ConstString) != -1) {       
  cout << ConstString << " is not in " << SearchString << endl;  } 

else {       
  cout << ConstString << " is in " << SearchString << endl;
  cout << "Details of the hit: " << endl;       
  cout << "Starting poisition of the hit: " << SearchString.Find(ConstString) << endl;                
  cout << "The matching substring is: " << SearchString.Substring(SearchString.Find(ConstString), ConstString.length()); }

再次,cin&gt;&gt;运算符完美运行,但请帮我弄清楚如何输出字符串。

1 个答案:

答案 0 :(得分:2)

不确定你在问什么。实现取决于您希望如何输出字符串。

对于某些原始输出,代码很可能如下所示:

output << rhs.get_data();