BillyYu

The means of Gotcha

From Wikipedia, the free encyclopedia

Gotcha is a Slang term derived from the phrase "I got you", usually referring to an unexpected capture or discovery. It may refer to an unexpected or unintuitive, but documented, behavior in a computer system( as opposed to a bug). It may also refer to many other things, they are omitted in this paper.
A gotcha is a detrimental condition (usually of a contract or agreement) that is designed to sneak past the other party. For example, many "free" Credit Report sites have "gotchas" that automatically sign you up for a monthly credit report service unless you explicitly cancel. "Gotcha" is also a frequently used programming term.
Programming gotchas

In programming, a gotcha is a feature of a system, a program or a programming language that works in the way it is documented but is counter-intuitive and almost invites mistakes because it is both enticingly easy to invoke and completely unexpected and/or unreasonable in its outcome.


Gotchas in the C programing language

Equality operator

The classic gotcha in C is the fact that

if (a=b) code;


is syntactically valid and sometimes even correct. It puts the value of b into a and then executes code if a is non-zero. What the programmer probably meant was

if (a==b) code;


which executes code if a and b are equal.

Function calls

In C and C++, function calls that need no arguments still require parentheses. If these are omitted the program will still compile, but will not produce the expected results. For example:

#include <iostream>
using namespace std;
int myfunc(){
return 
42;
}
int main (){
cout 
<< myfunc;
return 
0;
}


The program will only display the expected result (the string 42) if the form cout << myfunc(); is used. If the shown form cout << myfunc; is used, the address of the function myfunc is cast to a boolean value, and the program will instead display the string 1.

Gotchas in the C++ programing language

Initializer lists

In C++, it is the order of the class inheritance and of the member variables that determine the initialization order, not the order of an initializer list:

#include <iostream>
class CSomeClass
{
public:
CSomeClass(
int n)
{
std::cout 
<< "CSomeClass constructor with value ";
std::cout 
<< n << std::endl;
}
};
class CSomeOtherClass
{
public:
CSomeOtherClass() 
//In this example, despite the list order,
: obj2(
2), obj1(1//obj1 will be initialized before obj2.
{
//Do nothing.
}
private:
CSomeClass obj1;
CSomeClass obj2;
};
int main(void)
{
CSomeOtherClass obj;
return 
0;
}

posted on 2007-09-15 22:50 志华 阅读(991) 评论(4)  编辑 收藏 引用

评论

# re: The means of Gotcha 2007-09-15 23:09 Minidx全文检索

下面是一点参考:
如果你打球时假动作逼真,骗过了对手,球回到令对手意想不到的地方而得分,就可以自鸣得意的大叫一声 “Gotcha!”,意思是(I’ve)got you,骗到你了吧!不知大家记不记得电影《小鬼当家》,小机灵用玩具水枪吓得歹徒落荒而逃后,满脸童真的笑意,说了一句“Gotcha!”

如果你开玩笑,朋友却信以为真,也可以说Gotcha,骗到你了吧。

在电脑领域中尤其是编程语言里经常出现,到底是什么意思呢?

Gotcha是I’ve got you的省略语,为“难倒你了”、“问倒你了”的意思。

I got you.
我了解.

这句跟 I see, I understand, 都是一样的, 适合在跟美国同学讨论功课时使用. 例如有一次我跟我的美国同学说这题该怎么怎么作, 他就很高兴地说, Ok, now I got you. 你如果不说 I got you, 说 I got it 也是可以的, 这二个都很常听人家说.
 
另外 I got you 有一个更常用的解释, 就是我骗到你了, 通常会读成 Gotcha.. 比如说你骗人家说, I got married. 别人回答: Oh~ Really? 这时你就可以吹著口哨说.. Hahaha.. Gotcha.

C+ +长久演化与发展以来,这样一本gotchas书籍的迟迟出版显得有点怪怪的。C++作为一个现实可用的语言,已经有至少十五年的历史了;而C++标准在过去的五年中也几乎没有改变。就此你可能会认为,语言中的缺陷现在应该已经成为众人皆知的“常识(common knowledge)[注1]”了。然而,即使经验丰富的C++程序员也会时不时遇到麻烦;Steve Dewhurst——这位从业多年的C++课程教师、作者以及顾问——则比任何人都清楚这一点。Dewhurst看上去似乎更多的是在责备程序员的求次而安,而非C++的复杂性。在本书中,对C++基础议题的忽视及不良的风格被列为程序设计中的两大原罪。经验本身无法避免这样一些人为的失误;有时候我们确是需要一些刺耳忠言。据此,C++ Gotchas不仅仅是一本关于C++疑难杂症的目类书籍,也是现世警言,提醒我们去关注那些可能被我们忽视的问题。

惯用法便是上述问题之一。在口语中,所谓惯用法是指被经常使用的单语(比如“gotcha”),其能迅捷的表达一个明晰的含义。而惯用法的含义之所以明晰,则仅仅是因为该单语被广泛使用,而非其中的各单字的含义使然。在一门编程语言中,一个惯用法是指一个表达式或技巧,其能够明晰的表达程序员的意图。同样,编程语言中惯用法表意明晰之性质源自其被广泛使用的程度。一个没能学到口语惯用法的人会处于非常不便的境地;而一个不使用编程惯用法的程序员则会导致其他所有程序员——特别是其代码维护者——的工作更难做。在Dewhurst的书中,恰当的运用惯用法被视为良好的编程风格,而良好的编程风格可以最大程度的减少gotchas出现。  回复  更多评论   

# re: The means of Gotcha 2007-09-16 07:49 志华

原来有了这样一本书出来了啊,有机会的话,倒可以好好的研究研究!!谢谢Minidx,发现你的blog上好多东西,要好好的学习下
^_^@Minidx全文检索
  回复  更多评论   

# re: The means of Gotcha 2007-09-17 00:07 turing

gotcha一般可以译为“陷阱”。  回复  更多评论   

# re: The means of Gotcha 2007-09-17 15:57 力为

I got you~  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜