C++ Primer Plus的string类的简单实现

来源:互联网 发布:淘宝钻展设计 编辑:程序博客网 时间:2024/06/14 19:19

重载构造函数,特别是重载复制构造函数。

为什么要重载复制构造函数? 在给string变量声明的时候,如 string str = str2; 编译器会自动生成复制构造函数,从而不会调用其他的构造函数,导致调用析构函数的次数增多。

重载运算符  = 

友元重载运算符  > < = >> <<



////  stringBad.h//  Class_String////  Created by Ben_22 on 14-5-14.//  Copyright (c) 2014年 Ben_22. All rights reserved.//#ifndef __Class_String__stringBad__#define __Class_String__stringBad__#include <iostream>using std::ostream;using std::istream;#endif /* defined(__Class_String__stringBad__) */class String{private:    char *str;    size_t len;    static int num_strings;public:    static const int CINLIM = 80;    String(const char *s);    String();    String(const String &);    ~String();        size_t length()const {return len;}        String & operator=(const String &);    String & operator=(const char *);        char & operator[](int i);    const char & operator[](int i )const;        friend bool operator<(const String &st, const String &st2);    friend bool operator>(const String &st, 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, const String &st);        static int HowMany();};



////  stringBad.cpp//  Class_String////  Created by Ben_22 on 14-5-14.//  Copyright (c) 2014年 Ben_22. All rights reserved.//#include <cstring>#include "stringBad.h"using std::cout;using std::cin;int String::num_strings = 0;int String::HowMany(){    return num_strings;}String::String(const char*s){    len = std::strlen(s);    str = new char[len+1];    std::strcpy(str, s);    num_strings++;}String::String(){    len = 0;    str = new char[1];    str[0] = '\0';    num_strings++;}String::String(const String &st){    num_strings ++;    len = st.len;    str = new char [len+1];    std::strcpy(str, st.str);}String::~String(){    --num_strings;    delete []str;}String &String::operator=(const String &st){    if (this == &st) {        return *this;    }    delete []str;    len = st.len;    str = new char[len+1];    std::strcpy(str, st.str);    return *this;}String &String::operator=(const char* s){    delete []str;    len = std::strlen(s);    str = new char[len+1];    std::strcpy(str, s);    return *this;}char &String::operator[](int i){    return str[i];}const char & String::operator[](int i )const {    return str[i];}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){    return (std::strcmp(st1.str, st2.str) == 0);}ostream &operator<<(ostream &os, const String &st){    os<< st.str;    return os;}istream & operator>>(istream &is, String &st){    char temp[String::CINLIM];    is.get(temp, String::CINLIM);    if (is){        st = temp;    }    while (is && is.get()!='\n') {        continue;    }        return is;}



0 0