copy construction note

#include <iostream>
using std::ostream;
using std::istream;

class String
{
private:
    char * str;
    int len;
    static int num_strings;
    static const int CINILIM = 80;
public:
    String(const char * s);   //constructor
    String();
    String(const String &);   //copy constructor
    ~String();
    int length() const { return len; }
    //operator overloading
    String & operator=(const String & st);
    String & operator=(const char * s);
    char & operator[](int i);
    const char & operator[](int i) const;
    //friends
    friend bool operator<(const String &st1, const String &st2);
    friend bool operator>(const String &st1, const String &st2);
    friend bool operator==(const String &st1, const String &st2);
    friend std::ostream & operator<< (std::ostream & os, const String & st);
    friend std::istream & operator>>(std::istream & is, const String & st);
    //static
    static int HowMany();
str0.h
String::String(const String & st)
{
num_strings++;
len = st.len;
str = new char[len + 1];
strcpy(str, st.str); // 在公有函数中的类对象,是可以访问private 数据
}


posted @ 2018-03-22 19:42  Bear_Guo  阅读(143)  评论(0编辑  收藏  举报