各位看下这个运算符重载的题咋做


#include <iostream>
#include <string>
#include <malloc.h>
#include <string.h> 

using namespace std;

class String{
private:
    char * s;
public:
    String();
    String(const char *);
    String(const String &);
    ~String();
    String & operator=(const char *);
    String & operator=(const String &);
    String operator+(const char *);
    String operator+(const String &);
    String & operator+=(const char *);
    String & operator+=(const String &);
    friend istream & operator>>(istream &, String &);
    friend ostream & operator<<(ostream &, const String &);
    friend bool operator==(const String &, const char *);
    friend bool operator==(const String &, const String &);
    friend bool operator!=(const String &, const char *);
    friend bool operator!=(const String &, const String &);
};

String::String(){
    s = NULL;
}

String::String(const char *s){
    this->s = (char*)s;
}

String::String(const String &s){
    this->s = (char*)s.s;
}

String::~String(){}

String & String::operator=(const char *c){
    static String s = String("1");
    static String s2 = String("0");
    if (this->s == c)
        return s;
    else
        return s2;
}

String & String::operator=(const String &s){
    static String s3 = String("1");
    static String s2 = String("0");
    if (this->s == s.s)
        return s3;
    else
        return s2;
}

String String::operator+(const char *c){
    if (this->s == "")
        this->s = (char *)c;
    else{
        char *name = (char *)malloc(strlen(this->s) + strlen(c));
        strcpy(name, this->s);
        strcat(name, c);
        this->s = name;
    }
    return *this;
}

String String::operator+(const String &s){
    if (this->s == "")
            this->s = (char *)s.s;
    else{
        char *name = (char *)malloc(strlen(this->s) + strlen(s.s));
        strcpy(name, this->s);
        strcat(name, s.s);
        this->s = name;
    }
    return *this;
}

String & String::operator+=(const char *s){
    if (this->s == "")
        this->s = (char *)s;
    else{
        char *name = (char *)malloc(strlen(this->s) + strlen(s));
        strcpy(name, this->s);
        strcat(name,s);
        this->s = name;
    }
    return *this;
}

String & String::operator+=(const String &s){
    if (this->s == "")
        this->s = (char *)s.s;
    else{
        char *name = (char *)malloc(strlen(this->s) + strlen(s.s));
        strcpy(name, this->s);
        strcat(name, s.s);
        this->s = name;
    }
    return *this;
}

istream & operator>>(istream &input, String &s){
    string str;
    input >> str;
    s.s = (char*)str.c_str();
    cout << s.s <<endl;
    return input;
}

ostream & operator<<(ostream &output, const String &s){
    output << s.s;
    return output;
}

bool operator==(const String &s, const char *c){
    if (s.s == c)
        return true;
    return false;
}

bool operator==(const String &s, const String &c){
    if (s.s == c.s)
        return true;
    return false;
}

bool operator!=(const String &s, const char *c){
    if (s.s != c)
        return true;
    return false;
}

bool operator!=(const String &s, const String &c){
    if (s.s != c.s)
        return true;
    return false;
}


int main()
{
    String s;  
    s += "hello";
    cout << s << endl;
    String s1("String1");
    String s2("copy of "); 
    s2 += "String1";
    cout << s1 << "\n" << s2 << endl;
    String s3;
    cin >> s3;
    cout << s3 << endl;
    String s4("String4"), s5(s4);
    cout << (s5 == s4) << endl;
    cout << (s5 != s4) << endl;
    String s6("End of "), s7("my string.");
    s6 += s7;
    cout << s6 << endl;
    return 0;
}

img


运行之后咋没有任何效果?