img winmain

Obfuscated C++

发表于2001/11/27 17:27:00  599人阅读


Obfuscated C++

作者:Robert Murray


Email – ccplusplus@21cn.com





#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]


int main(){

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

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

return 0;








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.

0 0



取 消