实现 C++字符串运算符
我有一个关于 char*、字符串相加的问题 像这样:
enter code here
s2 = s3 + "," + s1;
我下面有三个运算符
friend Mystring operator+( const Mystring &lhs, const Mystring &rhs); -- 1
friend Mystring operator+( const Mystring &mystr, const char *ch ); -- 2
friend Mystring operator+( const char *ch, const Mystring &mystr ); -- 3
,但我使用 1 和 3 它会崩溃,但我使用 1 和 3 可以做得很好。
我的问题是顺序不是 s3 + "," 首先,所以首先使用运算符 w 结果使用运算符3,但事实并非如我所想。
谁能解释为什么会发生这种情况?
Mystring operator+( const Mystring &mystr,const char *ch )
{
Mystring tmp;
tmp.str_ = new char[ strlen(mystr.str_)+2 ];
strcpy( tmp.str_, mystr.str_ );
strcat( tmp.str_, ch );
return tmp;
}
Mystring operator+( const char *ch, const Mystring &mystr )
{
Mystring tmp;
tmp.str_ = new char[ strlen(mystr.str_)+strlen(mystr.str_)+1 ];
strcpy( tmp.str_, mystr.str_ );
strcat( tmp.str_, mystr.str_ );
return tmp;
}
Mystring operator+( const Mystring &lhs, const Mystring &rhs )
{
Mystring tmp;
tmp.str_ = new char[ strlen(lhs.str_)+strlen(rhs.str_)+1 ];
strcpy( tmp.str_, lhs.str_ );
strcat( tmp.str_, rhs.str_ );
return tmp;
}
I have a problem about char*, string add together
such like this:
enter code here
s2 = s3 + "," + s1;
and I have three operator below
friend Mystring operator+( const Mystring &lhs, const Mystring &rhs); -- 1
friend Mystring operator+( const Mystring &mystr, const char *ch ); -- 2
friend Mystring operator+( const char *ch, const Mystring &mystr ); -- 3
but I use 1 and 3 it will crash, but I use 1 and 3 can do good.
My problem is the order isn't that s3 + "," first so use operator w first
and the result use operator 3, but the fact isn't as my thought.
Can anyone explain why this happens?
Mystring operator+( const Mystring &mystr,const char *ch )
{
Mystring tmp;
tmp.str_ = new char[ strlen(mystr.str_)+2 ];
strcpy( tmp.str_, mystr.str_ );
strcat( tmp.str_, ch );
return tmp;
}
Mystring operator+( const char *ch, const Mystring &mystr )
{
Mystring tmp;
tmp.str_ = new char[ strlen(mystr.str_)+strlen(mystr.str_)+1 ];
strcpy( tmp.str_, mystr.str_ );
strcat( tmp.str_, mystr.str_ );
return tmp;
}
Mystring operator+( const Mystring &lhs, const Mystring &rhs )
{
Mystring tmp;
tmp.str_ = new char[ strlen(lhs.str_)+strlen(rhs.str_)+1 ];
strcpy( tmp.str_, lhs.str_ );
strcat( tmp.str_, rhs.str_ );
return tmp;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先尝试测试更简单的事情:
然后再进行链接:
这样您就可以更清楚地判断问题所在。
Try testing simpler things first:
before moving on to chaining:
that way you can tell what the issue is more clearly.
在
你应该写:
在这里:
你应该写:
In
you should write:
And here:
you should write:
你读过这篇文章吗? 什么是三法则?
基于您手动管理内存的方式这些功能,我认为可以肯定地假设您没有。不管怎样,一旦你正确地实施了三巨头,这就是你应该做的。首先你应该有两个完整的成员。一种用于存储字符串的大小,另一种用于存储动态数组的容量。然后,您需要一个私有函数,我们将其称为
increase_capacity
,用于在必要时增加容量。所有的内存分配都将在一个地方进行。它将大大简化事情。它可能看起来像这样:现在,您还应该有一个调整大小功能,可以在必要时调整大小。
比上面所有的更好,您应该只使用
vector
,但也许您正在尝试了解手动内存管理,这很好。现在,您应该实现operator+=,作为成员:
如果您愿意,您还可以实现一个采用const char * 的运算符,这将节省依赖隐式转换时可能发生的分配。
最后,您可以实现您的
operator+
:如果您有一个来自
const char *
的隐式转换,那么它应该涵盖所有这些。但是,如果您编写了采用const char *
的额外operator+=
来减少分配次数,那么您可能还应该编写采用const char * 的操作符+=
在它的右侧。看起来和上面的一样,只是第二个参数的类型改变了。您不需要为反向操作编写一个,因为无论如何 lhs 都需要分配为 Mystring。Have you read this? What is The Rule of Three?
Based on the way you manually manage memory in these functions, I think it's safe to assume that you haven't. Anyway, once you do properly implement the big three, here is what you should do. First you should have two integral members. One to store the size of the string, and one to store the capacity of the dynamic array. Then you want to have a private function, let's call it
increase_capacity
, that increases the capacity if necessary. All your memory allocation will take place there, in one spot. It will simplify things greatly. It might look something like this:Now, you should also have a resize function, that adjusts the size, if necessary.
Better than all that above, you should just use
vector<char>
, but maybe you're trying to understand manual memory management, that's fine.Now, you should implement operator+=, as a member:
If you want, you can also implement one that takes
const char *
, that will save an allocation that would happen if you relied on the implicit conversion.Finally, you can implement your
operator+
:If you have an implicit conversion from
const char *
, that should cover all of them. But if you wrote the extraoperator+=
that takes aconst char *
in order to do fewer allocations, you should probably also write the one that takesconst char *
on it's right side. It looks the same as the one above, just the type of the second parameter changes. You shouldn't need to write one for the reverse operation, since the lhs will need to be allocated as a Mystring anyway.