Bjarne Stroustrup's C++ Style and Technique FAQ
Modified December 17, 2011
These are questions about C++ Style and Technique 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.
For more general questions, see my
general FAQ.
For terminology and concepts, see my
C++ glossary.
Please note that these are just a collection of questions and answers. They are not
a substitute for a carefully selected sequence of examples and explanations
as you would find in a good textbook. Nor do they offer detailed and precise
specifications as you would find in a reference manual or the standard.
See
The Design and Evolution of C++ for questions
related to the design of C++.
See The C++ Programming Language for questions
about the use of C++ and its standard library.
Translations:
-
Chinese
of some of this Q&A with annotations - another Chinese version.
- Japanese
- Ukrainian
- Topics:
- Getting started
- Classes
- Hierarchy
- Templates and generic programming
- Memory
- Exceptions
- Other language features
- Trivia and style
- Getting started:
- How do I write this very simple program?
- Can you recommend a coding standard?
- How do I read a string from input?
- How do I convert an integer to a string?
- Classes:
- How are C++ objects laid out in memory?
- Why is "this" not a reference?
- Why is the size of an empty class not zero?
- How do I define an in-class constant?
- Why isn't the destructor called at the end of scope?
- Does "friend" violate encapsulation?
- Why doesn't my constructor work right?
- Class hierarchies:
- Why do my compiles take so long?
- Why do I have to put the data in my class declarations?
- Why are member functions not virtual by default?
- Why don't we have virtual constructors?
- Why are destructors not virtual by default?
- What is a pure virtual function?
- Why doesn't C++ have a final keyword?
- Can I call a virtual function from a constructor?
- Can I stop people deriving from my class?
- Why doesn't C++ have a universal class Object?
- Do we really need multiple inheritance?
- Why doesn't overloading work for derived classes?
- Can I use "new" just as in Java?
- Templates and generic programming:
- Why can't I define constraints for my template parameters?
- Why can't I assign a vector<Apple> to a vector<Fruit>?
- Is "generics" what templates should have been?
- why use sort() when we have "good old qsort()"?
- What is a function object?
- What is an auto_ptr and why isn't there an auto_array?
- Why doesn't C++ provide heterogenous containers?
- Why are the standard containers so slow?
- Memory:
- How do I deal with memory leaks?
- Why doesn't C++ have an equivalent to realloc()?
- What is the difference between new and malloc()?
- Can I mix C-style and C++ style allocation and deallocation?
- Why must I use a cast to convert from void*?
- Is there a "placement delete"?
- Why doesn't delete zero out its operand?
- What's wrong with arrays?
- Exceptions:
- Why use exceptions?
- How do I use exceptions?
- Why can't I resume after catching an exception?
- Why doesn't C++ provide a "finally" construct?
- Can I throw an exception from a constructor? From a destructor?
- What shouldn't I use exceptions for?
- Other language features:
- Can I write "void main()"?
- Why can't I overload dot, ::, sizeof, etc.?
- Can I define my own operators?
- How do I call a C function from C++?
- How do I call a C++ function from C?
- Why does C++ have both pointers and references?
- Should I use NULL or 0?
- What's the value of i++ + i++?
- Why are some things left undefined in C++?
- What good is static_cast?
- So, what's wrong with using macros?
- Trivia and style:
- How do you pronounce "cout"?
- How do you pronounce "char"?
- Is ``int* p;'' right or is ``int *p;'' right?
- Which layout style is best for my code?
- How do you name variables? Do you recommend "Hungarian"?
- Should I use call-by-value or call-by-reference?
- Should I put "const" before or after the type?