简体   繁体   English

运算符 + 重载不起作用

[英]Operator+ overloading is not working

I just developed a String class and overloaded =, <<, [] operators but my operator+ is not working, a little help with it please!我刚刚开发了一个 String 类并重载了 =、<<、[] 运算符,但我的运算符 + 不起作用,请帮忙!

Class:班级:

class String
{
private:
    int length;
    char *chars;
public:
    String oprator+(String &obj);
}

Main Code:主要代码:

int main ( )
{
    String str1;
    str1 = "This is first text";
    String str2;
    str2 = "This is second text";

    String bigString = str1 + str2;
    bigString = bigString + "This is third text";
}

operator+ overload:运算符+重载:

String::String operator+ (String &obj)
{
    String *temp = new String();
    strcpy(temp->chars, chars);
    strcat(temp->chars, obj.chars);
    return *temp;
}

Your operator+ should read您的operator+应该阅读

class String
{
private:
    int length;
    char *chars;
    String(const char* s, size_t n):
        length( n ),
        chars( new char[n] )
    {
        std::copy(s, s+length, chars);
    }

public:
    explicit String(size_t l):
        length(l),
        chars( new char[l] )
    {
    }

    String(char const* s):
        String(s, strlen(s) )
    {
    }

    String(String const& s):
        String(s.chars, s.length)
    {
    }

    String& operator=(String s)
    {
        std::swap(chars, s.chars);
        std::swap(length, s.length);
        return *this;   
    }

    ~String() {delete[] chars;}

    template<size_t N> String(const char s[N]):
        String(s, N)
    {
    }

    void append(String const& s)
    {
        char* tmp = new char[length + s.length];
        std::copy(chars, chars+length, tmp);
        std::copy(s.chars, s.chars + s.length, tmp + length);
        delete[] chars;
        chars = tmp;
    }

    template<typename S> friend S& operator<<(S&, String const&);
};

String operator+(String const& s1, String const& s2)
{
    String merged(s1);
    merged.append(s2);
    return merged;
}

template<typename S> S& operator<<(S& stream, String const& s)
{
    return stream << s.chars;
}

int main()
{
  String s("bla");
  std::cout << s << std::endl;
  String s2 = s + "bla";
  std::cout << s2 << std::endl;
}

You don't want to modify the argument, so it should be a const reference.您不想修改参数,因此它应该是一个常量引用。 Making it non-const prevents code like使它成为非常量可以防止代码像

bigString = bigString + "This is third text";

because the create temporary String, if you add a non-explicit constructor, cannot bind to an l-value reference.因为创建临时字符串,如果添加非显式构造函数,则无法绑定到左值引用。

And the operator should not be a member function, but a free function to take advantage of conversions on the first argument.并且操作符不应该是一个成员函数,而应该是一个自由函数来利用第一个参数的转换。 With a free function, you can do有了免费的功能,你可以做到

bigString = "This is third text" + bigString;

which is not possible with the member function because char const[] does not have an operator+ which takes a String .这在成员函数中是不可能的,因为char const[]没有采用Stringoperator+

PS: You may want to read Monoliths "Unstrung" for some critic of std::string's interface. PS:对于 std::string 接口的一些批评者,您可能想阅读Monoliths "Unstrung"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM