Obfuscated C++

 

Obfuscated C++

作者:Robert Murray

译者:袁小凯

Email – ccplusplus@21cn.com


译序

在我看到这个题目后,就想试着写出其输出。结果和我写的相同,但是,我没有喜乐,而是一脸的迷惑。因为我不知道如何用我有限的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.

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论
在本书中,C++专家Rob Murray就与我们分享了他宝贵的经验和建议,以帮助初中级C++程序员得到进一步的提高。 在本书中,作者大量采用了实际开发中的代码来作为示例,向读者展示了那些有用的编程策略,并对那些有害的做法进行了警示。为了帮助读者更好地理解,在书中的每一章结束前,在该章中被介绍过的主要内容都被放到了一个列表中,此外,书中还给出了一些问题来激励读者们进行更多的思考和讨论。 本书在一开始就向我们讲解了如何为我们的设计选择正确的抽象,提示我们注意抽象和现实之间的区别。然后,我们就将学到如何将已得到的抽象转化成一个(或多个)C++中的类,期间进行的讨论所涵盖的范围上至高层的设计策略,下至底层的接口和实现细节。 接下来本书对单继承和多重继承进行了深入的探索。一开始书中会给出一个关于它们应该用在设计的什么地方的讨论,然后就是一些详细的示例代码,用来向我们演示如何在实践中使用这些概念。对于“如何构建可派生出其他类的类”以及“这么做的好处何在”,书中还专门抽出了一章来讨论它们。 对于C++中新增的模板特性,通过从基础开始到逐步地接触实际应用中的示例,Rob Murray向我们展示了其空前的洞察力。作者同时也向我们展示了多种特定的技巧,以使我们的程序更快、重用性更高,并且更健壮。异常是C++中另外一个新增的特性,对于何时该使用它,何时不该使用它,Murray也向我们给出了他的建议。在本书的最后,我们还可以学到如何将一个项目从C移植到C++之上,书中对该过程的讨论不但包括了其中可能出现的技术问题,也包括了使用技术的“人”的问题。 作者简介 Robert B.Murray在撰写该书时是量子数据系统公司(Quantitative Data Systems)中负责软件工程的副总裁,该公司的业务包括向财富500强(Fortune 500)中的公司提供面向对象的软件解决方案。在此之前,他曾经在AT & T的Bell实验室工作,在那里他参与了C++语言、编译器以及库的开发。他同时也是《The C++ Report》杂志的创立编辑(founding editor),主持Obfuscated C++专栏。从1987年起,他就开始在学术会议和专业会议上向人教授C++语言。目前Murray在FNIS公司进行着软件开发的管理,他的兴趣主要包括:程序语言的开发和工具,以及轻量级的开发过程(如极限编程)。 王昕,自1995年起接触C++编辑,参与过多个C++项目的开发。曾在网上和杂志上发表过多篇有关C++的文章。主要兴趣包括:程序语言的开发、形式化理论、软件开发方法学等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

winmain

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值