2006C++之父BS最新大作:Bjarne Stroustrup's FAQ (先报道,即将翻译了)

这是Bjarne Stroustrup经常被问到的问题集合,包括他对C++、面向对象编程和泛型编程的看法,以及对其他语言如Java、C#和C++/CLI的评价。他还讨论了为何C++允许不安全的代码,推荐的学习资源,以及他的著作和C++标准的相关信息。
摘要由CSDN通过智能技术生成
Bjarne Stroustrup's FAQ

These are questions that people ask me often. If you have better questions or comments on the answers, feel free to email me bs@research.att.com. Please remember that I can't spend all of my time improving my homepages.

This page concentrates on personal opinions and general questions related to philosophy. For questions that more directly relate to C++ language features and the use of C++, see my C++ style and technique FAQ. For C++ terminology and concepts, see my C++ glossary. For links to useful sources of C++ information, see my C++ page. For information about my books (incl. reviews and support information), see my book list. For papers and ISBNs for translations of my books, see my publication list.

Some of my FAQ has been translated into Chinese; see here (traditional) or here.
Index

   * How do you pronounce "Bjarne Stroustrup"?
   * Can I ask you a question?
   * Why don't you answer your email?
   * Why don't you make your website look modern?

   * What's so great about classes?
   * What is "OOP" and what's so great about it?
   * What is "generic programming" and what's so great about it?
   * Why does C++ allow unsafe code?

   * What is the best book to learn C++ from?
   * How long does it take to learn C++?
   * Knowing C is a prerequisite for learning C++, right?
   * Should I learn a pure OO language before C++ to become a real OO programmer?
   * How do I start learning C++?
   * Will you help me with my homework?

   * Where can I get a free C++ compiler?
   * You are Swedish?
   * What's the best way to improve my C++ programs?
   * Does it matter which programming language I use?

   * Did the ANSI/ISO standards committee spoil C++?
   * When will we have a C++ standard?
   * Where can I get a machine-readable version of the standard?
   * Are there any features you'd like to remove from C++?
   * What will C++0x look like?

   * When will we have a new ARM?
   * When will you publish a 4th edition of "The C++ Programming Language"?
   * Should I buy the "Special Edition" of "The C++ Programming Language"?
   * Have you really sold X00,000 copies of TC++PL?
   * Where do I find free machine-readable copies of your books?
   * What C++ compiler do you recommend? Which libraries?

   * Is Java the language you would have designed if you didn't have to be compatible with C?
   * What do you think of C#?
   * What do you think of C++/CLI?
   * Why are you so keen on portability?
   * Do you really recommend Ada over C++ for larger projects?
   * Would you compare C++ to "some language"?
   * Others do compare their languages to C++; doesn't that annoy you?

   * You won't compare C++ to other languages, but you write diatribes about C++?
   * C is better than C++ for small projects, right?
   * Is C a subset of C++?
   * What is the difference between C and C++?
   * Do you really think that C and C++ could be merged into a single language?

   * When was C++ invented?
   * Why did you invent C++?
   * Why did AT&T support the development of C++?
   * Do you own C++?
   * Where did the name "C++" come from?
   * Which language did you use to write C++?

   * Why doesn't C++ have garbage collection?
   * Why doesn't C++ have a GUI?
   * Why doesn't C++ support threads?

   * Why is the code generated for the "Hello world" program ten times larger for C++ than for C?
   * How can a legacy language like C++ compete with modern, advanced languages?
   * What is "multiparadigm programming"?
   * Why is C++ so BIG?

   * What do you think of EC++?
   * C++ got its Object-Oriented concepts from Smalltalk?
   * Is C++ an Object-Oriented language?
   * Did you really say that?

   * Did you really give an interview to IEEE?
   * What is "legacy code"?
   * Is the number of C++ users still doubling every year?
   * Does anyone use C++ these days?
   * Did you expect C++ to become such a success?
   * What's a good certification for C++ programmers?
   * What are you working on now?

How do you pronounce "Bjarne Stroustrup?"
It can be difficult for non-Scandinavians. The best suggestion I have heard yet was "start by saying it a few times in Norwegian, then stuff a potato down your throat and do it again :-)" Here is a wav file.

For people who can't receive sound, here is a suggestion: Both of my names are pronounced with two syllables: Bjar-ne Strou-strup. Neither the B nor the J in my first name are stressed and the NE is rather weak so maybe Be-ar-neh or By-ar-ne would give an idea. The first U in my second name really should have been a V making the first syllable end far down the throat: Strov-strup. The second U is a bit like the OO in OOP, but still short; maybe Strov-stroop will give an idea.

Yes, this probably is the most frequently asked question :-)

P.S. My first name is Bjarne - not Bjorn (not a name), Bj?rn (a related but different name), nor Barney (an unrelated name). My second name is Stroustrup - not Stroustroup, Stroustrop, Strustrup, Strustrop, Strustroup, Straustrup, nor Straustroup (documents using each of these misspellings can be found using google).

Can I ask you a question?
Certainly. I try to answer my email. However, please try to avoid asking a question that is answered in my homepages. Also, please don't rely on a prompt answer. I get a lot of email.

Here are links to

   * biographical information
   * C++ information and links
   * papers (incl. a few that can be downloaded)
   * books (incl. reviews, errata, and a few chapters that can be downloaded)
   * interviews

Why don't you answer your email?
I do, but I get a lot of email. I estimate that I reply to more than 95% of the (non spam) messages I receive. However, sometimes I get overwhelmed. Some messages are lost in my mailbox, some are delayed until I can find time, some are delayed until I get around to answering a set of related messages (this often happens to comments about potential errors in my books). Unfortunately, longer and more thoughtful messages are more likely to get delayed than simple ones that have simple answers.

Also, if you mail me, please try to make sure that I can reply to you. I really hate it when I have written and sent a reply, just to find that the return address is invalid or inaccessible. This happens to me every week.

Two kinds of messages have a relatively high chance of getting lost: homework questions and questions of the form "how do I use this proprietary library?". I'm a bit sad about not answering the latter questions because often the person asking doesn't understand that the DOS, Windows, or whatever interface from C++ is not part of the C++ standard (and I cannot keep up with the huge number of C++ libraries). If you fail to receive an answer, please consider if your question was of one of these kinds.

Why don't you make your website look modern?
I'm a "contents provider" not a website designer. I can use my time to improve the contents or the looks, but not both.

What looks "cool and modern" to someone is often considered bad taste by someone else, and fashions change fast. Also, very plain html downloads and displays faster than anything else, and many people still suffer from slow web connections.

What is so great about classes?

Classes are there to help you organize your code and to reason about your programs. You could roughly equivalently say that classes are there to help you avoid making mistakes and to help you find bugs after you so make a mistake. In this way classes significantly helps maintenance.

A class is the representation of an idea, a concept, in the code. An object of a class represents a particular example of the idea in the code. Without classes, a reader of the code would have to guess about the relationships among data items and functions - classes make such relationships explicit and "understood" by compilers. With classes, more of the high-level structure of your program is reflected in the code, not just in the comments.

A well-designed class presents a clean and simple interface to its users, hiding its representation and saving its users from having to know about that representation. If the representation shouldn't be hidden - say, because users should be able to change any data member any way they like - you can think of that class as "just a plain old data structure"; for example:

struct Pair {
      Pair(const string& n, const string& v) : name(n), value() { }
      string name, value;
};

Note that even data structures can benefit from auxiliary functions, such as constructors.

When designing a class, it is often useful to consider what's true for every object of the class and at all times. Such a property is called an invariant. For example, the invariant of a vector could be that its representation consists of a pointer to a number of elements and that number of elements is stored in an integer. It is the job of every constructor to establish the class invariant, so that every member function can rely on it. Every member function must leave the invariant valid upon exit. This kind of thinking is particularly useful for classes that manage resources, such as locks, sockets, and files. For example, a file handle class will have the invariant that it holds a pointer to an open file. The file handle constructor opens the file. Destructors free resources acquired by constructors. For example, the destructor for a file handle closes the file opened by the constructor:

cass File_handle {
File_handle(const char* n, const char* rw)
{ f = fopen(n,rw); if (f==0) throw Open_failure(n); }
~File_handle() { fclose(f); } // destructor
// ...
private:
FILE* f;
};

If you haven't programmed with classes, you will find parts of this explanation obscure and you'll underestimate the usefulness of classes. Look for examples. Like all good textbooks, TC++PL has lots of examples; for example, see A Tour of the Standard Library. Most modern C++ libraries consist (among other things) of classes and a library tutorial is one of the best places to look for examples of useful classes.

What is "OOP" and what's so great about it?

There are lots of definitions of "object oriented", "object-oriented programming", and "object-oriented programming languages". For a longish explanation of what I think of as "object oriented", read Why C++ isn't just and object-oriented programming language. That said, object-oriented programming is a style of programming originating with Simula (about 40 years ago!) relying of encapsulation, inheritance, and polymorphism. In the context of C++ (and many other languages with their roots in Simula), it means programming using class hierarchies and virtual functions to allow manipulation of objects of a variety of types through well-defined interfaces and to allow a program to be extended incrementally through derivation.

See What's so great about classes? for an idea about what great about "plain classes". The point about arranging classes into a class hierarchy is to express hierarchical relationships among classes and use those relationships to simplify code.

To really understand OOP, look for some examples. For example, you might have two (or more) device drivers with a common interface:

class Driver { // common driver interface
public:
virtual int read(char* p, int n) = 0; // read max n characters from device to p
// return the number of characters read
virtual bool reset() = 0; // reset device
virtual Status check() = 0; // read status
};

This Driver is simply an interface. It is defined with no data members and a set of pure virtual functions. A Driver can be used through this interface and many different kinds of drivers can impement this interface:

class Driver1 : public Driver { // a driver
public:
Driver1(Register); // constructor
int read(char*, int n);
bool reset();
Status check();
// implementation details
};

class Driver2 : public Driver { // another driver
public:
Driver2(Register);
int read(char*, int n);
bool reset();
Status check();
// implementation details
};

Note that these drivers hold data (state) and objects of them can be created. They implement the functions defined in Driver. We can imagine a driver being used like this:

void f(Driver& d) // use driver
{
Status old_status = d.check();
// ...
d.reset();
char buf[512];
int x = d.read(buf,512);
// ...
}

The key point here is that f() doesn't need to know which kind of driver it uses; all it needs to know is that it is passed a Driver; that is, an interface to many different kinds of drivers. We could invoke f() like this:

void g)
{
Driver1 d1(Register(0xf00)); // create a Driver1 for device
// with device register at address 0xf00

Driver2 d2(Register(0xa00)); // create a Driver2 for device
// with device register at address 0xa00
// ...
int dev;
cin >> dev;

if  (i=1)
f(d1); // use d1
else
f(d2); // use d2
// ...
}

Note that when f() uses a Driver the right kind of operations are implicitly chosen at run time. For example, when f() is passed d1, d.read() uses Driver1::read(), whereas when f() is passed d2, d.read() uses Driver2::read(). This is sometimes called run-time dispatch or dynamic dispatch. In this case there is no way that f() could know the kind of device it is called with because we choose it based on an input.

Please note that object-oriented programming is not a panacea. "OOP" does not simply mean "good" - if there are no inherent hierarchical relationships among the fundamental concepts in your problem then no amount of hierarchy and virtual functions will improve your code. The strength of OOP is that there are many problems that can be usefully expressed using class hierarchies - the main weakness of OOP is that too many people try to force too many problems into a hierarchical mould. Not every program should be object-oriented. As alternatives, consider plain classes, generic programming, and free-standing functions (as in math, C, and Fortran).

What is "generic programming" and what's so great about it?

Generic programming is programming based on parameterization: You can parameterize a type with another (such as a vector with its element types) and an algorithm with another (such as a sort function with a comparison function). The aim of generic programming is to generalize a useful algorithm or data structure to its most general and useful form. For example, a vector of integers is fine and so is a function that finds the largest value in a vector of integers. However, a
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值