C with class

Class is a type

  • Compile-time access control
  • static type checking
  • function definition under class, make a class more like an interface specification.
  • new/delete

C++ Language Design Rules

Aims

  • C++ makes programming more enjoyable for serious programmers
  • C++ is a general-purpose programming language that
    • is a better C
    • supports data abstraction
    • supports object-oriented programming

General Rules

  • C++’s evolution must be driven by real problems
  • Don’t get involved in a sterile quest for perfection
  • C++ must be useful now
  • Every feature must have a reasonably obvious implementation
  • Always provide a transition path
  • C++ is a language, not a complete system
  • Provide comprehensive support for each supported style
  • Don’t try to force people

Design Support Rules

  • Support sound design notions
  • Provider facilities for program organization
  • Say what you mean
  • All features must be affordable
  • It is more important to allow a useful feature than to prevent every misuse
  • Support composition of software from separately developed parts

Language-Technical Rules

  • No implicit violations of the static type system
  • Provide as good support for user-defined types as for built-in types
  • Locality is good
  • Avoid order dependencies
  • If in doubt, pick the variant of a feature that is easiest to teach
  • Syntax matters(often in perverse ways)
  • Preprocessor usage should be eliminated

Low-Level Programming Support Rules

  • Use traditional(dumb) linkers
  • No gratuitous incompatibilities with C
  • Leave no room for a lower-level language below C++(except assembler)
  • What you don’t use, you don’t pay for(zero-overhead rule)
  • If in doubt, provide means for manual control

Standardization

对于一个被广泛使用的语言,一个统一标准是必须的,简单来说,标准定义了什么是general的,什么是implementation-dependent。

标准委员会的组成非常复杂,有各种职业者,各个公司代表。

委员会的很多工作,不为大多数人所知,而且非常无聊。感觉与程序员的修养相悖,那些花了很多时间探究和定义的写法问题,恰恰是程序员不关心的,我们更多地关注最佳实践,而不是令人目眩的奇怪语法。

Examples

  • Name look up - Namespace - Redefinition
  • Lifetime of temporaries - EOS
  • Keyword argument
  • Restricted pointer
  • Character sets

Libraries

Design Tradeoffs

  • Emphasize run-time efficiency?
  • Minimize recompilation after a change?
  • Maximize portability across platforms?
  • Enable users to extend the basic library?
  • Allow user without source code available?
  • Blend in with existing notations and styles?
  • Be usable from code not written in C++?
  • Be usable by novices?

Major Extensions

Run-Time Type information

  • dynamic_cast
    • It has a static check: the two objects must have derivation relation.
    • It doesn’t require the target type is exactly what the obj is. The target type can be a parent class.
  • typeid()
    • You should try to avoid typeid() in a switch statement. In most cases, virtual function is more scalable.

Templates

Exception Handling

Namespaces