在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
这不是一个奇怪的功能,事实上,如果你仔细想想,它是完全有意义的,但还是给了我一个WTF时刻。
在c++(和c#)中,基类的子类不能访问基实例上的私有和受保护成员。
class Base {
protected:
m_fooBar;
};
class Derived: public Base {
public:
void Test(Base& baseInstance) {
m_fooBar=1; //OK
baseInstance.m_fooBar = 1; //Badness
//This, however is OK:
((Derived&)baseInstance).m_fooBar = 1; //OK
}
};
其他回答
我在试图找出一个完全没有意义但无论如何都能工作的MACRO时遇到了这个。这对于objective-c是正确的,但对于其他类型的C(或者至少是gcc编译器)也可能是正确的。
NSString *oneString = @"This " @"is " @"just " @"one " @"normal " @" string";
=
NSString *oneString = @"This is just one normal string";
C风格的字符串也是如此
char* str = "this " "also " "works";
其余的这些都没有令人震惊的Ruby触发器操作符:
p = proc {|a,b| a..b ? "yes" : "no" }
p[false,false] => "no"
p[true,false] => "yes"
p[false,false] => "yes" # ???
p[false,true] => "yes"
p[false,false] => "no"
是的,程序状态存储在解释器的解析树中。这就是为什么开发一个兼容的Ruby实现要花很长时间的原因。但是我原谅你,Ruby
PHP
来自在线文档: string implode (string $glue, array $pieces) -用字符串连接数组元素 注意:由于历史原因,implode()可以以任意一种顺序接受其形参。
这是可行的:implode($someArray, $glue)
希望他们能在PHP 6中消除这些历史怪癖。
在SQL Server中,如果你在生产代码中使用select *,你可能会遇到一个令人讨厌的意外。无论如何,使用select *并不被认为是一个好的实践,但了解一些有趣的行为是很好的。
参见问题“select * from table”vs“select colA,colB etc from table”在SqlServer2005中有趣的行为了解更多细节
在Bash中,变量可以显示为标量和数组:
$ a=3
$ echo $a
3
$ echo ${a[@]} # treat it like an array
3
$ declare -p a # but it's not
declare -- a="3"
$ a[1]=4 # treat it like an array
$ echo $a # acts like it's scalar
3
$ echo ${a[@]} # but it's not
3 4
$ declare -p a
declare -a a='([0]="3" [1]="4")'
$ a=5 # treat it like a scalar
$ echo $a # acts like it's scalar
5
$ echo ${a[@]} # but it's not
5 4
$ declare -p a
declare -a a='([0]="5" [1]="4")'
KSH做同样的事情,但是使用排版而不是声明。
当你在zsh中这样做时,你得到的是子字符串赋值而不是数组:
$ a=3
$ a[2]=4 # zsh is one-indexed by default
$ echo $a
34
$ a[3]=567
$ echo $a
34567
$ a[3]=9
$ echo $a
34967
$ a[3]=123 # here it overwrites the first character, but inserts the others
$ echo $a
3412367
$ a=(1 2 3)
$ echo $a
1 2 3 # it's an array without needing to use ${a[@]} (but it will work)
$ a[2]=99 # what about assignments?
$ echo $a
1 99 3