当前位置:嗨网首页>书籍在线阅读

12-进一步重载赋值运算符

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

12.2.5 进一步重载赋值运算符

介绍针对String类的程序清单之前,先来考虑另一个问题。假设要将常规字符串复制到String对象中。例如,假设使用getline()读取了一个字符串,并要将这个字符串放置到String对象中,前面定义的类方法让您能够这样编写代码:

String name;
char temp[40];
cin.getline(temp, 40);
name = temp; // use constructor to convert type

但如果经常需要这样做,这将不是一种理想的解决方案。为解释其原因,先来回顾一下最后一条语句是怎样工作的。

1.程序使用构造函数String(const char *)来创建一个临时String对象,其中包含temp中的字符串副本。第11章介绍过,只有一个参数的构造函数被用作转换函数。

2.本章后面的程序清单12.6中的程序使用String & String::operator=(const String &)函数将临时对象中的信息复制到name对象中。

3.程序调用析构函数~String()删除临时对象。

为提高处理效率,最简单的方法是重载赋值运算符,使之能够直接使用常规字符串,这样就不用创建和删除临时对象了。下面是一种可能的实现:

String & String::operator=(const char * s)
{
    delete [] str;
    len = std::strlen(s);
    str = new char[len + 1];
    std::strcpy(str, s);
    return *this;
}

一般说来,必须释放str指向的内存,并为新字符串分配足够的内存。

程序清单12.4列出了修订后的类声明。除了前面提到过的修改之外,这里还定义了一个CINLIM常量,用于实现operator>>()。

程序清单12.4 string1.h

// string1.h -- fixed and augmented string class definition
#ifndef STRING1_H_
#define STRING1_H_
#include <iostream>
using std::ostream;
using std::istream;
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();
};
#endif

程序清单12.5给出了修订后的方法定义。

程序清单12.5 string1.cpp

// string1.cpp -- String class methods
#include <cstring>              // string.h for some
#include "string1.h"            // includes <iostream>
using std::cin;
using std::cout;
// initializing static class member
int String::num_strings = 0;
// static method
int String::HowMany()
{
    return num_strings;
}
// class methods
String::String(const char * s)   // construct String from C string
{
    len = std::strlen(s);        // set size
    str = new char[len + 1];     // allot storage
    std::strcpy(str, s);         // initialize pointer
    num_strings++;               // set object count
}
String::String()                 // default constructor
{
    len = 4;
    str = new char[1];
    str[0] = '\0'; // default string
    num_strings++;
}
String::String(const String & st)
{
    num_strings++;            // handle static member update
    len = st.len;             // same length
    str = new char [len + 1]; // allot space
    std::strcpy(str, st.str); // copy string to new location
}
String::~String()  // necessary destructor
{
    --num_strings; // required
    delete [] str; // required
}
// 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;
    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);
    str = new char[len + 1];
    std::strcpy(str, s);
    return *this;
}
    // read-write char access for non-const String
char & String::operator[](int i)
{
    return str[i];
}
    // read-only char access for const String
const char & String::operator[](int i) const
{
    return 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 st2 < st1;
}
bool operator==(const String &st1, const String &st2)
{
    return (std::strcmp(st1.str, st2.str) == 0);
}
    // simple String output
ostream & operator<<(ostream & os, const String & st)
{
    os << st.str;
    return os;
}
    // quick and dirty String input
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;
}

重载>>运算符提供了一种将键盘输入行读入 String 对象中的简单方法。它假定输入的字符数不多于String::CINLIM的字符数,并丢弃多余的字符。在if条件下,如果由于某种原因(如到达文件尾或get(char *, int)读取的是一个空行)导致输入失败,istream对象的值将置为false。

程序清单12.6通过一个小程序来使用这个类,该程序允许输入几个字符串。程序首先提示用户输入,然后将用户输入的字符串存储到String对象中,并显示它们,最后指出哪个字符串最短、哪个字符串按字母顺序排在最前面。

程序清单12.6 sayings1.cpp

// sayings1.cpp -- using expanded String class
// compile with string1.cpp
#include <iostream>
#include "string1.h"
const int ArSize = 10;
const int MaxLen =81;
int main()
{
    using std::cout;
    using std::cin;
    using std::endl;
    String name;
    cout <<"Hi, what's your name?\n>> ";
    cin >> name;
    cout << name << ", please enter up to " << ArSize
         << " short sayings <empty line to quit>:\n";
    String sayings[ArSize]; // array of objects
    char temp[MaxLen]; // temporary string storage
    int i;
    for (i = 0; i < ArSize; i++)
    {
        cout << i+1 << ": ";
        cin.get(temp, MaxLen);
        while (cin && cin.get() != '\n')
            continue;
        if (!cin || temp[0] == '\0') // empty line?
            break;                   // i not incremented
        else
            sayings[i] = temp;       // overloaded assignment
    }
    int total = i;                   // total # of lines read
    if ( total > 0)
    {
        cout << "Here are your sayings:\n";
        for (i = 0; i < total; i++)
            cout << sayings[i][0] << ": " << sayings[i] << endl;
        int shortest = 0;
        int first = 0;
        for (i = 1; i < total; i++)
        {
            if (sayings[i].length() < sayings[shortest].length())
                shortest = i;
            if (sayings[i] < sayings[first])
                first = i;
        }
        cout << "Shortest saying:\n" << sayings[shortest] << endl;
        cout << "First alphabetically:\n" << sayings[first] << endl;
        cout << "This program used "<< String::HowMany()
             << " String objects. Bye.\n";
    }
    else
        cout << "No input! Bye.\n";
    return 0;
}

注意: 较早的get(char *, int)版本在读取空行后,返回的值不为false。然而,对于这些版本来说,如果读取了一个空行,则字符串中第一个字符将是一个空字符。这个示例使用了下述代码:

if (!cin || temp[0] == '\0') // empty line?
    break;                   // i not incremented

如果实现遵循了最新的C++标准,则if语句中的第一个条件将检测到空行,第二个条件用于旧版本实现中检测空行。

程序清单12.6中程序要求用户输入至多10条谚语。每条谚语都被读到一个临时字符数组,然后被复制到String对象中。如果用户输入空行,break语句将终止输入循环。显示用户的输入后,程序使用成员函数length()和operator <()来确定最短的字符串以及按字母顺序排列在最前面的字符串。程序还使用下标运算符([])提取每条谚语的第一个字符,并将其放在该谚语的最前面。下面是运行情况:

Hi, what's your name?
>> Misty Gutz
Misty Gutz, please enter up to 10 short sayings <empty line to quit>:
1: a fool and his money are soon parted
2: penny wise, pound foolish
3: the love of money is the root of much evil
4: out of sight, out of mind
5: absence makes the heart grow fonder
6: absinthe makes the hart grow fonder
7:
Here are your sayings:
a: a fool and his money are soon parted
p: penny wise, pound foolish
t: the love of money is the root of much evil
o: out of sight, out of mind
a: absence makes the heart grow fonder
a: absinthe makes the hart grow fonder
Shortest saying:
penny wise, pound foolish
First alphabetically:
a fool and his money are soon parted
This program used 11 String objects. Bye.