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

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 limit
public:
    // 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_strings
int 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 String
String & 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 String
String & 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 String
char & String::operator[](int i)
{
    if(i >=0 && i <= strlen(str))
       return str[i];
}
 
// read-only char access for const String
const char & String::operator[](int i) const
{
    return static_cast<const char>(str[i]);  //此处与上一空内容一样
}
 
// overloaded operator friends
bool 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 output
ostream & operator<<(ostream & os, const String & st)
{
    os <<  st.str <<" " << st.len << " " << st.num_strings << std::endl;
    return os;
}
 
// quick and dirty String input
istream & operator>>(istream & is, String & st)
{
    is >> st.len >> st.str;
    return is;
}
 
int main()
{
   String s1 = "abd";
   String s2 = s1;
   cout << s2[1];
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值