需要某种运算符.. c ++ [英] need some kind of operator.. c++

查看:67
本文介绍了需要某种运算符.. c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串序列存储在队列中.如果我使用成员函数 push()

I want to store a sequence of string in a queue. This seems pretty simple if i use the member function push()

queue test;
string s0("s0"), s1("s1");

test.push(s0);
test.push(s1);

我正在考虑以隐式方式将字符串添加到队列中.这意味着,如果我输入以下字符串序列,例如 operator>> 应该将字符串值推送到队列中.

I am thinking about adding the strings in the queue in implicitly way. That mean, if I type the following sequence of string the e.g. operator >> should push the string value in the queue.

queue test;
string s0("s0"), s1("s1");

s0 >> s1 >> s2 >> s3 ;

有什么办法吗?

推荐答案

尽管C ++不允许这样做,但它允许您执行非常类似的操作: test<<s0<<s1; 但是, 不要这样做!

While C++ doesn't allow you this, it allows you to do something very similar: test << s0 << s1; However, don't do this!

如果我看到 test.push(s0),我完全知道它的作用,甚至不用查看 test 的类型.如果我看到 test<<s0<<s1; ,我认为 test 是写入的流.

If I see test.push(s0), I know exactly what it does, without even looking at the type of test. If I see test << s0 << s1;, I'd think test is a stream that's written to.

这是我的重载运算符时应遵循的三个基本规则:

  1. 尽管看似明显的相反证据,但仅在少数情况下,才适合使用运算符重载.因此,重载运算符的首要原则(实际上)说:不要这样做.这似乎很奇怪,但是原因是实际上很难理解其背后的语义.操作员的应用程序,除非在应用程序域中对操作符的使用是众所周知的且无可争议.与普遍的看法相反,情况并非如此. 只要运算符的含义不明显且无争议,就不应重载. 而是提供一个函数,并为其选择适当的名称.
  2. C ++对重载运算符的语义几乎没有限制.您的编译器将很乐意接受实现二进制 + 运算符的代码,以更改其正确的操作数.但是,这种运算符的用户永远不会怀疑表达式 a + b 会更改 b 的值.这就是为什么运算符重载的第二条规则说: 始终坚持运算符的众所周知的语义. (这又假定语义是无可争议的;请参见以前的规则.)
  3. 最后,请始终记住运算符彼此之间以及与其他运算之间是相关的.如果您的类型支持 a + b ,则用户也希望能够调用 a + = b .如果它支持前缀增量++ a,他们也会希望a ++也能工作.如果他们可以检查 a<b ,他们肯定会希望能够检查 a>b .如果他们可以复制构造您的类型,则希望分配也能正常工作.因此,运算符重载的第三条规则提醒您: 总是提供一组相关操作中的全部.
  1. Despite seemingly obvious contrary evidence, there only are surprisingly few cases where operator overloading is appropriate. Consequentially, the first and foremost rule for overloading operators, at its very heart, says: Don’t do it. That might seem strange, but the reason is that actually it is hard to understand the semantics behind the application of an operator unless the use of the operator in the application domain is well known and undisputed. Contrary to popular believe, this is hardly ever the case. Whenever the meaning of an operator is not obviously clear and undisputed, it should not be overloaded. Instead, provide a function with a well-chosen name.
  2. C++ poses few limitations on the semantics of overloaded operators. Your compiler will happily accept code that implements the binary + operator to change its right operand. However, the users of such an operator would never suspect the expression a + b to change the value of b. This is why the second rule of operator overloading says: Always stick to the operator’s well-known semantics. (This, in turn, presumes that the semantics are undisputed; see the previous rule.)
  3. Finally, always remember that operators are related to each other and to other operations. If your type supports a + b, users will expect to be able to call a += b, too. If it supports prefix increment ++a, they will expect a++ to work, too. If they can check whether a < b, they will most certainly expect to also to be able to check whether a > b. If they can copy-construct your type, they expect assignment to work as well. So the third rule of operator overloading reminds you: Always provide all out of a set of related operations.


与所有此类规则一样,确实存在例外.有时人们偏离了他们,结果不是很糟糕的代码,但是这种偏离很少而且相差很远.至少在我看到的100个这样的偏差中,有99个是不合理的.但是,也可能是千分之九千.因此,您最好遵循这些规则.


As with all such rules, there are indeed exceptions. Sometimes people have deviated from them and the outcome was not bad code, but such deviations are few and far between. At the very least, 99 out of 100 such deviations I have seen were unjustified. However, it might just as well have been 999 out of 1000. So you’d better stick to these rules.

以防万一我还不够清楚:对于您问题中的代码,背离这些规则是非常糟糕的.

So just in case I hadn't been clear enough: For the code from your question, a deviation from these rules is very bad.

这篇关于需要某种运算符.. c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆