在C ++中设计点积运算符 [英] designing a dot product operator in 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 :
- 如果运算符的含义不明确,则不应重载.
- 始终符合操作员的众所周知的语义.
- 始终为一组相关运算符的 all 提供重载.
- If the meaning of an operator is ambigious, it should not be overloaded.
- Always conform to the operator's well-known semantics.
- 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屋!