我有以下课程: -
class myclass
{
size_t st;
myclass(size_t pst)
{
st=pst;
}
operator int()
{
return (int)st;
}
int operator+(int intojb)
{
return int(st) + intobj;
}
};
Run Code Online (Sandbox Code Playgroud)
只要我像这样使用它,这个工作正常: -
char* src="This is test string";
int i= myclass(strlen(src)) + 100;
Run Code Online (Sandbox Code Playgroud)
但我无法做到这一点: -
int i= 100+ myclass(strlen(src));
Run Code Online (Sandbox Code Playgroud)
任何想法,我怎么能实现这个?
我理解行为上的差异.Date()返回表示当前日期的String,并new Date()返回我可以调用其方法的Date对象的实例.
但我不知道为什么.JavaScript是原型,因此Date是一个函数和一个具有成员函数(方法)的对象,它们也是对象.但我没有编写或阅读任何行为方式的JavaScript,我想了解其中的区别.
有人可以向我展示一个具有方法的函数的示例代码,使用new运算符返回一个实例,并在直接调用时输出一个String吗?即这样的事情是怎么发生的?
Date(); // returns "Fri Aug 27 2010 12:45:39 GMT-0700 (PDT)"
new Date(); // returns Object
new Date().getFullYear(); // returns 2010
Date().getFullYear(); // throws exception!
Run Code Online (Sandbox Code Playgroud)
非常具体的要求,我知道.我希望这是件好事.:)
所以univ运营商.我并不完全理解.
例如:
foo(PredList,[H|_]) :- bar(PredList,H).
foo(PredList,[_|T]) :- foo(PredList,T),!.
bar([H|_],Item) :- G =.. [H,Item],G.
bar([_|T],Item) :- bar(T,Item).
Run Code Online (Sandbox Code Playgroud)
这是做什么的?这样可以查看另一个谓词是否为真.我不明白".."的作用.
如果没有univ运算符,你会如何重写它?
我试图变得懒惰并在抽象基类中实现转换运算符,而不是在每个派生的具体类中实现.我已经成功地投了一条路,但是我无法施展另一条路.我认为这可能是不可能的,但是在放弃之前想要选择集体的思想:
public interface IValueType<T>
{
T Value{ get; set; }
}
public abstract class ValueType<T> : IValueType<T> {
public abstract T Value { get; set; }
public static explicit operator T(ValueType<T> vt) {
if(vt == null)
return default(T);
return vt.Value;
}
public static implicit operator ValueType<T>(T val) {
ValueType<T> vt = new ValueType<T>(); //<--- obviously this won't work as its abstract
vt.Value = val;
return vt;
}
}
Run Code Online (Sandbox Code Playgroud) 这对于那里的C++大师来说.
请考虑以下代码:
class X { };
template <class T>
class Mistake {
public:
T x;
Mistake(const T& k) : x(k) { }
Mistake(const X&) : x(1) { }
void print() { cout << x << endl; }
};
template <class T>
Mistake<T> operator+(const Mistake<T>& a, const Mistake<T>& b)
{
return Mistake<T>(a.x + b.x);
}
Run Code Online (Sandbox Code Playgroud)
我有一个类"错误",我想要进行加法操作.当我尝试:
X a, b;
Mistake<int> foo = a + b;
Run Code Online (Sandbox Code Playgroud)
我收到编译错误; 编译器似乎无法意识到必须实例化模板运算符+.
另一方面,如果我之前添加以下代码:
Mistake<int> operator+(const Mistake<int>& a, const Mistake<int>& b)
{
return Mistake<int>(a.x + b.x);
} …Run Code Online (Sandbox Code Playgroud) 可能重复:
非成员运算符重载应放在何处?
在浏览SO时,我经常会发现涉及重载/定义a std::ostream& operator<<(std::ostream& os, const Foo& foo)或a的问题或答案Foo operator+(const Foo& l, const Foo& r).
虽然我知道如何以及何时(不)编写这些操作符,但我对namespace此事感到困惑.
如果我有以下课程:
namespace bar
{
class Foo {};
}
Run Code Online (Sandbox Code Playgroud)
namespace我应该在哪个中编写不同的运算符定义?
// Should it be this
namespace bar
{
std::ostream& operator<<(std::ostream& os, const Foo& foo);
}
// Or this ?
namespace std
{
ostream& operator<<(ostream& os, const bar::Foo& foo);
}
// Or this ?
std::ostream& operator<<(std::ostream& os, const bar::Foo& foo);
Run Code Online (Sandbox Code Playgroud)
同样的问题适用于operator+.那么,这里的好习惯是什么?为什么?
为了使这项工作,我需要改变什么?
class A:
@staticmethod
def __getitem__(val):
return "It works"
print A[0]
Run Code Online (Sandbox Code Playgroud)
请注意,我__getitem__在类型上调用方法A.
我在这里得到了一些PHP代码:
<?php
echo 'hello ' . 1 + 2 . '34';
?>
Run Code Online (Sandbox Code Playgroud)
输出234,
但是当我在"你好"之前添加一个数字11时:
<?php
echo '11hello ' . 1 + 2 . '34';
?>
Run Code Online (Sandbox Code Playgroud)
它输出1334而不是245(我预期它),为什么呢?
php string concatenation operator-precedence operator-keyword
根据维基百科,二元运算符?:是
通俗地称为猫王操作员,因为它与表情符号相似.
我的问题是:
operator-keyword ×10
c++ ×3
python ×2
backtracking ×1
c# ×1
function ×1
generics ×1
javascript ×1
namespaces ×1
naming ×1
new-operator ×1
operators ×1
overloading ×1
php ×1
prolog ×1
static ×1
string ×1
syntax ×1
templates ×1