在C ++中设计点积运算符 [英] designing a dot product operator in c++

查看:76
本文介绍了在C ++中设计点积运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够编写类似a = b.c的语句;其中b和c是std :: vector,a是它们的标量点积(双精度).为此,我应将点积运算符与."关联.象征.这可能吗?

I would like to be able to write statements like a = b.c; where b and c are std::vector and a is their scalar dot product (double). For doing this, I should associate the dot product operation with '.' symbol. Is this possible?

推荐答案

您不能覆盖点运算符(.).

You cannot override the dot-operator (.).

此外,即使您可能永远也不应为此目的.覆盖运算符时,应遵循以下三个规则:

Moreover, even if you could you never should for this purpose. When overriding operators, you should follow these three rules :

  1. 如果运算符的含义不明确,则不应重载.
  2. 始终符合操作员的众所周知的语义.
  3. 始终为一组相关运算符的 all 提供重载.
  1. If the meaning of an operator is ambigious, it should not be overloaded.
  2. Always conform to the operator's well-known semantics.
  3. Always provide overloads for all of a related group of operators.

您的预期设计违反了第二条规则:您希望彻底改变点运算符的语义.

Your intended design violates the second rule: you wish to drastically change the semantics of the dot-operator.

您应该提供自己的函数来执行点运算,而不是这样做.

Instead of doing this, you should provide your own function to perform the dot operation.

这篇关于在C ++中设计点积运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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