Programming 版 (精华区)
发信人: zhangyan (数电 我心中永远的痛), 信区: Programming
标 题: Q&A with C++ Creator Bjarne Stroustrup
发信站: 哈工大紫丁香 (2001年06月26日10:18:25 星期二), 站内信件
Question: I think C++ is the best programming language except for one
little detail. Couldn't you have simplified the string manipulation? Why
did you choose to make the array count start in 0 and not in 1?
Answer: Thanks. Well, I did make string manipulation much easier than in
C. Have you tried the standard library string class? Consider:
#include<string>
#include<iostream>
using namespace std;
int main()
{
cout << "Please enter user id:\n";
string id;
cin >> id;
cout << "Please enter destination:\n";
string dest;
cin >> dest;
string addr = id + '@' + dest;
// …
}
Try writing that in, say, C, without messing with memory management,
buffer overflow, etc.
Did you mean why didn't I add pattern matching to the language? There
are nice libraries for that (see the C++ libraries link on my C++ page),
but none of those are currently part of the standard library.
Why didn't I start arrays from 1 like in Fortran? Because I find that
most algorithms are cleaner with sequences starting from 0, and also
because I didn't have a choice: Dennis Ritchie had picked 0 as the start
of arrays for C long before I arrived on the scene.
Question: What is the default return type of a function in C++?
Answer: There is no default function return type in standard C++. In C89
and pre-standard C++, a function returned an "int" if the user didn't
specify anything else. This caused serious parsing problems and confused
novices who—reasonably—assumed that when they didn't specify a return
type, no value would be returned. C99 followed C++ in this, so there is
now no default return type in C either.
Question: I have a question and a comment. The question is this: Is it
possible to overload the array subscript operator [] so that it
functions for a two-dimensional array? For example:
class someClass;
someClass[ 0 ][ 1 ] = someValue;
I haven't found any information on this.
My comment was with regard to your book, The C++ Programming Language. I
can't even say how many times I've revisited the book and each time
come away with another valuable piece of knowledge. Thanks for the
time and energy required to put together such an informative reference.
Answer: The short answers are "yes, you can" and "thanks"!
The C++ Programming Language [Addison-Wesley, 2000, ISBN
0-201-70073-5] was written to provide long-term help to the reader. It's
not a book meant to be exhausted by a quick read through.
I generally prefer to access two-dimensional arrays by the standard
mathematical and Fortran-style a(1,2) notation, which is trivially
provided by defining operator()(int,int) for an array class. If you want
the C-style notation, you need to provide an operator[](int) for an
array class which returns something that represents a one-dimensional
array. You then define operator[](int) for that one-dimensional array to
be able to write a[2][3]. This would then be resolved as a.
operator[](2).operator[](3). Optimizers can do a good job with such
code, so that you can get a[2][3] evaluated without any function call
overhead. Have a look at The C++ Programming Language, Third Edition
[Addison-Wesley, 1997, ISBN 0-201-88954-4], sec22.4.6, for an example of
how to do this. There, I use valarrays, but the technique is general.
Question: In my sixth semester B.E., I need to do a project. Can you
suggest a project using C and C++?
Answer: Hmm. It's always hard to suggest projects. I think you should
look for something that will make you comfortable with the facilities of
the standard library. This points to projects that involve some text
manipulation and possibly some text I/O to files. As an example, I
recently decided to add a glossary of C++ terms to my home pages. For
that, I wrote a little program that took a simple list of terms
(possibly several words), definitions (possibly several lines), and
generated an XTML document with automatically generated cross-links.
HTML generation for some information you care about might be a good
project. Another example might be a program that defines and manipulates
a file of article references and comments, useful for keeping
research notes.
Question: How can I protect a class from inheritance? How can I make
sure that no other class can inherit my class?
Answer: In my experience, most attempts to prevent derivation from a
class are misguided. Either a class is designed (with virtual
functions and such) to be derived from, and then it should be left open.
Or a class is designed (without virtual functions) not to be used as
a base class, and then it's pretty obvious that you shouldn't derive
from it. If you feel the need for compile-time enforcement, declare a
destructor private. See the Usable_lock example in The Design and
Evolution of C++ [Addison-Wesley, 1994, ISBN 0-201-54330-3], sec11.4.3.
Question: What kind of knowledge is necessary to write a language? For
example, how much do you need to know about the hardware , flip flops,
and compiler construction, and do you need to know the whole computer
circuitry?
Answer: To design a programming language, you need to know quite a lot
about compiler construction and a bit about machine architecture and
operating systems. However, the most important thing is to have a
clear view of what kind of problems the languages is supposed to solve.
Successful languages are invariably designed by people who have
expert-level knowledge of their language's application domains.
Question: What's the best way to learn C++ on your own? What are the
best materials/books, tutorials, and software to use?
Answer: That's a difficult and often-asked question. The answer strongly
depends on your background and what you intend to use C++ for.
If you're already a programmer in some other language, I recommend the
Third Edition of my book, The C++ Programming Language [Addison-Wesley,
1997, ISBN 0-201-88954-4], and also Andrew Koenig and Barbara E.
Moo's Accelerated C++: Practical Programming by Example [Addison-Wesley,
2000, ISBN 0-201-70353-X]. My book is a thorough introduction to
language facilities and fundamental techniques, and can be used as a
user-level reference for years. Accelerated C++ is currently the best
introduction to modern C++ style and techniques.
In general, when you look for C++ books, visit the Association of C
and C++ Users (ACCU) site and look at their book reviews and
recommendations. This site is unique in that reviews are written by
professional programmers and that the ACCU is completely independent
of publishers, software vendors, etc.
When learning C++, focus on concepts and techniques, and learn
language technicalities as needed. Don't try to learn C first, if you
don't absolutely have to. Learning C is not a prerequisite for
learning C++, and C is not the ideal subset of C++ to learn first.
(See my article "Learning Standard C++ as a New Language"—available
from my home pages—for supporting arguments).
If you're not already a programmer, find a teacher/mentor and take
his/her advice. The advice and support of an experienced programmer is
far more important for initial learning than the choice of books,
software, programming language, etc.
Question: How do you declare a string as a variable without including
line size?
Answer:
string s;
Remember first to #include<string> somewhere. Don't mess with C-style
zero-terminated arrays if you aren't forced to. They're unnecessarily
complicated and error-prone to use.
Question: Since Java is a fairly complete language and Sun is very
keen on evolving it to new platforms and technologies, do you think it's
a good idea to stop looking at other languages?
Answer: It's never a good idea to stop looking at new languages. I use
C++ for all of my serious work that requires a general-purpose
programming language. However, I always try to find time to look at
new languages for ideas and inspiration. I don't find Java
particularly interesting, but I do look at it at times, even though it's
a proprietary language and I strongly prefer non-proprietary
languages (controlled by accredited standards bodies rather than
commercial corporations).
Question: Lots of people are parsing C++ code for various purposes
(smart editors, documentation systems, interpreters, syntax checkers,
diagnosis tools, and so on), using a plethora of tools from "simple"
regular expressions to Yacc-generated or handwritten parsers.
Will the standard library ever provide a mechanism to come to their
aid?
Answer: Sorry, I doubt it. The C++ grammar is such a mess that a
standard parser would be hard to provide.
Question: Which C++ compiler can be used to practice all (or most) of
the topics in your book The C++ Programming Language?
Answer: I use all the major compilers. Most are now close enough to
the standard to be useful for my purposes. Do get a recent release,
though, because a C++ compiler that's a couple of years old will be
far from standards-compliant. It breaks my heart when I get mail
explaining the trouble someone is having with Borland 3.1 or Microsoft
5. Those are venerable antiques that should have been retired years ago.
For starters, unless you have a modern compiler, you'll find using
the standard library difficult. And the standard library is one of the
greatest benefits of the standard. It makes learning to use C++ well
much easier than was previously feasible.
Modern compilers need not cost a fortune. In fact, several good ones are
available for free download. Look at my FAQ for details.
Question: Quoting you, "Also, where possible, prefer the standard
library to non-standard 'foundation libraries' and try to minimize use
of proprietary extensions." By Foundation libraries, do you mean
MFC-like classes? And can you explain the term proprietary extensions?
Answer: By "non-standard foundation libraries," I mean libraries
providing facilities such as containers that are also provided by the
standard library. Microsoft's MFC is one such example.
By "proprietary extensions," I mean language features not present in the
standard added by a vendor. Examples are Borland's "properties" and
Microsoft's __ prefixed keywords. Any use of such facilities ties you to
their provider
--
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 天外飞仙]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:212.584毫秒