Obfuscated C++

类别:编程语言 点击:0 评论:0 推荐:

 

Obfuscated C++

作者:Robert Murray

译者:袁小凯

Email – [email protected]

译序

在我看到这个题目后,就想试着写出其输出。结果和我写的相同,但是,我没有喜乐,而是一脸的迷惑。因为我不知道如何用我有限的C++知识去解释为什么,我只是猜对的,可悲吧!但看了结论后,一切都是那么的简单,那么明了。帖上这篇文章,让更多的人消除这该死的迷惑。但我自知水平有限,翻译的不好,也煅炼一下。请大家不要见笑,请多多批评。

正文

    以下是这段令人迷惑的C++程式:

#include <iostream>

using namespace std;

 

struct C;

 

struct D {

    void operator*(D) { cout << "one\n"; }

} C;

 

struct E {

    void operator*(E) { cout << "two\n"; }

} F;

 

struct F;

 

int main(){

    C* C;

    F* F;

    return 0;

}

这个难题的关健在于主函式main中那两条语句的含义。依照C++语法,看起来好像是定义两个指针。那空格是罪魁祸首,它极易使人产生这种误解。名字(C or F)在相同的作用范围(全局)内被同时声明为类名和对象名,这种情况下, 不管那个在先那个在后,对象名会屏蔽类名。使用类名时必须使用清楚的型别指示(如:struct C* C;)。注 [1]

    因此,函式main中的每条语句是一个两元operator*函式的调用。它和下面的程式有着相同的功效:

int main(){

    C.operator*(C); // Invokes D::operator*(D)

    F.operator*(F); // Invokes E::operator*(E)

return 0;

}

程式输出为:

one

two

 

这就是令人迷惑的原因,亲爱的C++朋友们!

作者后语:

This is my last "Obfuscated C++" Column. I've been writing this column for a bit over 11 years now, ever since the first issue of C++ Report was published in January 1989. C++ has grown over the years, as have I, and my life has taken me in other directions. C++ is no longer a part of what I do, so it's time to put this column to bed. I've had the pleasure of working with a lot of talented people over the years; you know who you are. Thanks for the ride, I'll never forget it!

注:

1.The class name can only be invoked using an elaborated type specifier (e.g. struct C* C;,).

 

About the Author

As Red Sky's Chief Technology Officer, Rob Murray is responsible for tracking and evaluating current and new Internet technologies. These technologies provide the "plumbing" that is critical to Web-based solutions that are full-featured, robust and fast. "Focus is key," he says. "New tools appear on the market every day. We identify and adopt those technologies that are the very best match for our skills and our customers' needs. Most don't make the cut." Before stepping into the CTO role, Mr. Murray was the Director of Systems Development for the Irvine office of Nuforia, where he was responsible for all fulfillment work in that office. An industry veteran, Mr. Murray spent the first 14 years of his career at AT&T Bell Laboratories, where he worked on object-oriented software and tools, including the first publicly released C++ compiler. He was the founding editor of the C++ Report in 1988, and has been a regular contributor to this respected trade magazine. Mr. Murray has been a prepublication reviewer for many technical textbooks, including important works by C++ inventor Bjarne Stroustrup and Java inventor James Gosling. His own book, C++ Strategies and Tactics (Addison-Wesley 1993), is currently in its seventh printing and has been translated into French and Japanese. His mission statement? "I have to keep three sets of people happy: my customers (who pay the bills); my employees and co-workers (who make successful projects happen); and my bosses (who sign my paycheck). One of the nice things about Red Sky is that if I take care of sets one and two, set three takes care of itself." Mr. Murray has a BS in Computer Science from Michigan State University, and a Master's in Computer Science from the University of Southern California.

本文地址:http://com.8s8s.com/it/it29860.htm