Last Month's Puzzle
Last month we asked you to predict the output of the following program:
#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; }This puzzle hinges on the meaning of the first two statements in the main program. Syntactically, these statements appear to be declarations of pointers. The white space is misleading. For each statement, the name (C or F) is declared both as a class type and as an object name in the same (global) scope. In this case, regardless of which declaration appears first, the object name hides the class name. The class name can only be invoked using an elaborated type specifier (e.g., struct C* C; ).
Each of these statements is therefore an application of binary operator*. We can rewrite the code as:
int main(){ C.operator*(C); // Invokes D::operator*(D) F.operator*(F); // Invokes E::operator*(E) return 0; }Our program therefore prints the output
one twoThat's All, Folks
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!
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.