实现string类过程中的一些疑问和总结

来源:互联网 发布:淘宝卖家怎么登录 编辑:程序博客网 时间:2024/06/05 14:35

1、为什么operator<< 和>>不能做为类的操作符重载呢,为什么一定还要声明为friend呢?

作为类的成员函数的时候,类对象默认为操作符的左参。
而你调用
cin >> obj
或者
cout << obj
的时候,类对象却是操作符的右参…

如果将operator<< 和>> 作为类的成员函数,那么在调用的时候就必须写成一种很奇怪

的样子: obj<<cout;

同时声明为friend可以方便操作类的成员变量。总结起来就是如下两点:

1 由于cout要在右边,所以<<和>>不能最为类的成员函数
2 如果在<<和>>中,不需要访问私有成员,就不用加firend


2、如下代码:

inline String& String::operator=(const String& other){    if (this!=&other)    {        delete[] m_data;        if(!other.m_data) m_data=0;        else        {            m_data = new char[strlen(other.m_data)+1];            strcpy(m_data,other.m_data);        }    }    return *this;}

inline:内联,减少栈的开销。具体请看我的另外一篇博文。

 m_data = new char[strlen(other.m_data)+1];

这里+1是因为strlen舍去了字符串结尾的 '\0' ,strcpy函数要根据  '\0' 判断结尾


string类的实现:

// mystring.h -- class definition#include <iostream>#include<string>using namespace std;class String{private:    char * str;             // pointer to string    int len;                // length of string    static int num_strings;  // number of objects    static const int CINLIM = 80;   // cin input limitpublic:    // constructors and other methods    String(const char * s);       // constructor    String();               // default constructor    String(const String &);   // copy constructor    ~String();              // destructor    int length () const { return len; }    // overloaded operator methods      String & operator=(const String &);    String & operator=(const char *);    char & operator[](int i);    const char & operator[](int i) const;    // overloaded operator friends    friend bool operator<(const String &st, const String &st2);    friend bool operator>(const String &st1, const String &st2);    friend bool operator==(const String &st, const String &st2);    friend ostream & operator<<(ostream & os, const String & st);    friend istream & operator>>(istream & is, String & st);    // static function    static int HowMany();}; // mystring.cpp -- String class methods // 初始化静态类成员num_stringsint String::num_strings = 0;// static method int String::HowMany(){    return num_strings;} // class methods,要求动态分配字符串内存空间String::String(const char * s) {    len = strlen(s);    if(!s) str = 0;    else    {        str = new char[len+1];         strcpy(str,s);    }} String::String()                 {    len = 4;    str = new char[1];    str[0] = '\0';            num_strings++;} String::String(const String & st):len(st.len){    if(!st.str) str = 0;    else    {        str = new char[len+1];         strcpy(str,st.str);    }} String::~String()                     // necessary destructor{    delete str;                } // overloaded operator methods  // assign a String to a StringString & String::operator=(const String & st){    if (this == &st)        return *this;    delete [] str;    len = st.len;    if(!st.str) str = 0;    else    {        str = new char[len + 1];        std::strcpy(str, st.str);    }    return *this;} // assign a C string to a StringString & String::operator=(const char * s){    delete [] str;    len = std::strlen(s);    if(!s) str = 0;    else    {        str = new char[len + 1];        std::strcpy(str, s);    }    return *this;} // read-write char access for non-const Stringchar & String::operator[](int i){    if(i >=0 && i <= strlen(str))       return str[i];} // read-only char access for const Stringconst char & String::operator[](int i) const{    return static_cast<const char>(str[i]);  //此处与上一空内容一样} // overloaded operator friendsbool operator<(const String &st1, const String &st2){    return (std::strcmp(st1.str, st2.str) < 0);} bool operator>(const String &st1, const String &st2){    return (std::strcmp(st1.str, st2.str) > 0);} bool operator==(const String &st1, const String &st2){    if(strlen(st1.str) != strlen(st2.str))        return false;    return strcmp(st1.str,st2.str) ? false:true;} // simple String outputostream & operator<<(ostream & os, const String & st){    os <<  st.str <<" " << st.len << " " << st.num_strings << std::endl;    return os;} // quick and dirty String inputistream & operator>>(istream & is, String & st){    is >> st.len >> st.str;    return is;} int main(){   String s1 = "abd";   String s2 = s1;   cout << s2[1];}





0 0
原创粉丝点击