哪些(如果有的话)语言可以覆盖原始类型的 + 运算符?
What (if any) languages can you override the + operator for primitive types?
正在根据脑力测试仪与一些朋友讨论问题是否如所写的那样有效。它引导我们讨论是否可以为任何语言的原始类型重载 +
或 -
。
是否有任何语言可以让您评估以下内容:
answer = 8 + 11;
if (answer == 96){
//Yay! It worked!
}
C#实现;不是完全你想要的(Trick
是不是原始类型),而是要求的行为:
public struct Trick {
public override bool Equals(object obj) {
return true;
}
public override int GetHashCode() {
return 0;
}
// Trick can be implicitly created from any int (e.g. from 19, 96 etc.)
public static implicit operator Trick(int value) {
return new Trick();
}
// All Trick instances are equal to each other
public static bool operator == (Trick left, Trick right) {
return true;
}
public static bool operator != (Trick left, Trick right) {
return false;
}
}
现在,我们准备测试 Trick
:
Trick answer;
// ------ Now goes your fragment -----------------------
answer = 8 + 11; // Trick instance can be implicitly created from int (from 19)
// .Net can't compare answer (which is Trick) and int (96), however
// Trick instance can be implicitly created from int (from 96)
// so .Net will create Trick instance from 96 and compare two Tricks
if (answer == 96) {
// when comparing two Trick instances:
// one created from 19 and other created from 96
// they are equal to one another
Console.Write("Yay! It worked!");
}
你会得到
"Yay! It worked!"
C++ 和 Ruby.
//C++
class X
{
public:
X& operator+=(const X& rhs) // compound assignment (does not need to be a member,
{ // but often is, to modify the private members)
/* addition of rhs to *this takes place here */
return *this; // return the result by reference
}
// friends defined inside class body are inline and are hidden from non-ADL lookup
friend X operator+(X lhs, // passing lhs by value helps optimize chained a+b+c
const X& rhs) // otherwise, both parameters may be const references
{
lhs += rhs; // reuse compound assignment
return lhs; // return the result by value (uses move constructor)
}
};
Ruby:
class String
def <<(value)
self.replace(value + self)
end
end
str = "Hello, "
str << "World."
puts str
正在根据脑力测试仪与一些朋友讨论问题是否如所写的那样有效。它引导我们讨论是否可以为任何语言的原始类型重载 +
或 -
。
是否有任何语言可以让您评估以下内容:
answer = 8 + 11;
if (answer == 96){
//Yay! It worked!
}
C#实现;不是完全你想要的(Trick
是不是原始类型),而是要求的行为:
public struct Trick {
public override bool Equals(object obj) {
return true;
}
public override int GetHashCode() {
return 0;
}
// Trick can be implicitly created from any int (e.g. from 19, 96 etc.)
public static implicit operator Trick(int value) {
return new Trick();
}
// All Trick instances are equal to each other
public static bool operator == (Trick left, Trick right) {
return true;
}
public static bool operator != (Trick left, Trick right) {
return false;
}
}
现在,我们准备测试 Trick
:
Trick answer;
// ------ Now goes your fragment -----------------------
answer = 8 + 11; // Trick instance can be implicitly created from int (from 19)
// .Net can't compare answer (which is Trick) and int (96), however
// Trick instance can be implicitly created from int (from 96)
// so .Net will create Trick instance from 96 and compare two Tricks
if (answer == 96) {
// when comparing two Trick instances:
// one created from 19 and other created from 96
// they are equal to one another
Console.Write("Yay! It worked!");
}
你会得到
"Yay! It worked!"
C++ 和 Ruby.
//C++
class X
{
public:
X& operator+=(const X& rhs) // compound assignment (does not need to be a member,
{ // but often is, to modify the private members)
/* addition of rhs to *this takes place here */
return *this; // return the result by reference
}
// friends defined inside class body are inline and are hidden from non-ADL lookup
friend X operator+(X lhs, // passing lhs by value helps optimize chained a+b+c
const X& rhs) // otherwise, both parameters may be const references
{
lhs += rhs; // reuse compound assignment
return lhs; // return the result by value (uses move constructor)
}
};
Ruby:
class String
def <<(value)
self.replace(value + self)
end
end
str = "Hello, "
str << "World."
puts str