我读到一个声明为成员函数的重载运算符是非对称的,因为它只能有一个参数而另一个自动传递的参数是this指针.所以没有比较它们的标准.另一方面,声明为a的重载运算符friend是对称的,因为我们传递两个相同类型的参数,因此可以对它们进行比较.
我的问题是,当我仍然可以将指针的左值与参考值进行比较时,为什么会选择朋友?(使用非对称版本提供与对称相同的结果)为什么STL算法仅使用对称版本?
c++ operator-overloading member-functions friend-function non-member-functions
如何以两种不同的方式为后缀a++和前缀重载operator ++ ++a?
有没有办法在TypeScript语言中进行方法重载?
我希望实现这样的目标:
class TestClass {
someMethod(stringParameter: string): void {
alert("Variant #1: stringParameter = " + stringParameter);
}
someMethod(numberParameter: number, stringParameter: string): void {
alert("Variant #2: numberParameter = " + numberParameter + ", stringParameter = " + stringParameter);
}
}
var testClass = new TestClass();
testClass.someMethod("string for v#1");
testClass.someMethod(12345, "string for v#2");
Run Code Online (Sandbox Code Playgroud)
这是我不想做的一个例子(我真的很讨厌在JS中重载hack的那部分):
class TestClass {
private someMethod_Overload_string(stringParameter: string): void {
// A lot of code could be here... I don't want to mix it with switch or if statement in …Run Code Online (Sandbox Code Playgroud) Python 2.x有两种方法可以重载比较运算符,__cmp__或者"丰富的比较运算符",如__lt__. 丰富的比较超载被认为是首选,但为什么会这样呢?
丰富的比较运算符更容易实现每个,但您必须使用几乎相同的逻辑实现其中几个.但是,如果你可以使用内置cmp和元组排序,那么__cmp__变得非常简单并完成所有的比较:
class A(object):
def __init__(self, name, age, other):
self.name = name
self.age = age
self.other = other
def __cmp__(self, other):
assert isinstance(other, A) # assumption for this example
return cmp((self.name, self.age, self.other),
(other.name, other.age, other.other))
Run Code Online (Sandbox Code Playgroud)
这种简单性似乎比重载所有6(!)丰富的比较更好地满足了我的需求.(但是,如果你依赖于"交换的论证"/反映的行为,你可以把它归结为"只是"4,但这导致并发症的净增加,在我的拙见中.)
如果我只是超载,是否有任何不可预见的陷阱需要注意__cmp__?
我明白了<,<=,==等运营商也可以被重载用于其他目的,并且可以返回任何他们喜欢的对象.我不是在询问这种方法的优点,而是仅仅考虑使用这些运算符进行比较时的差异,这与它们对数字的意义相同.
更新:克里斯托弗指出,cmp正在消失3.x. 有没有其他方法可以使实施比较变得如上所述__cmp__?
是否有可能使[]操作员重载两次?允许,像这样:( function[3][3]像在二维数组中).
如果有可能,我想看一些示例代码.
啊
#include "logic.h"
...
class A
{
friend ostream& operator<<(ostream&, A&);
...
};
Run Code Online (Sandbox Code Playgroud)
logic.cpp
#include "a.h"
...
ostream& logic::operator<<(ostream& os, A& a)
{
...
}
...
Run Code Online (Sandbox Code Playgroud)
当我编译时,它说:
std :: ostream&logic :: operator <<(std :: ostream&,A&)'必须只接受一个参数.
问题是什么?
我厌恶的C语言(作为一名数学家)就是这样的
(-1) % 8 // comes out as -1, and not 7
fmodf(-1,8) // fails similarly
Run Code Online (Sandbox Code Playgroud)
什么是最好的解决方案?
C++允许模板和运算符重载的可能性,但这些对我来说都是模糊的.感激地收到了例子.
能够超载它是非常有用的.C++中的operator并返回对象的引用.
你可以超载operator->,operator*但不能operator.
这有技术原因吗?
我似乎无法找到在javascript中重载[]运算符的方法.有人知道吗?
我在想......
MyClass.operator.lookup(index)
{
return myArray[index];
}
Run Code Online (Sandbox Code Playgroud)
或者我不是在看正确的事情.
我已经使用JavaScript了几天了,并且我想要为我定义的对象重载操作符.
在谷歌搜索这个问题后,似乎你无法正式做到这一点,但有一些人声称有一些冗长的方式来执行此操作.
基本上我已经制作了一个Vector2类,希望能够做到以下几点:
var x = new Vector2(10,10);
var y = new Vector2(10,10);
x += y; //This does not result in x being a vector with 20,20 as its x & y values.
Run Code Online (Sandbox Code Playgroud)
相反,我必须这样做:
var x = new Vector2(10,10);
var y = new Vector2(10,10);
x = x.add(y); //This results in x being a vector with 20,20 as its x & y values.
Run Code Online (Sandbox Code Playgroud)
我可以采用一种方法来重载Vector2类中的运算符吗?因为这看起来很丑陋.