[c++] Basic ideas and Style Guide

时间:2021-08-01 00:20:00

Get your own compiler:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install g++-4.9
you can then compile using the command:
g++-4.9 -std=c++14 -Wall -Werror -O2 -o helloworld helloworld.cpp


Style Rules:

Macros should never be required in C++.
Variables should be defined close to their use.
Don’t use malloc/realloc, use new (or smart pointers)
Minimise use of void*, pointer arithmetic, union and c-style casts
Minimise the use of C-style arrays and strings, use vector/array (from C++11) and string instead.

总之,忘掉C语言。


C++ Standard Library

For efficiency reasons, STL is not object-oriented:

    • Makes little use of inheritance, and
    • Makes no use of virtual functions

The Standard Template Library (STL) is a part of the C++ standard library, Some others are:

分类一:

  • The C standard library
  • Language support: <exception>, <ctime>, ...
  • Strings: <string>, <cstring>, ...
  • Numerics: <cmath>, <complex>, ...
  • I/O: <iostream>, <fstream>, <cstdio>, ...
  • Exception classes
  • Smart pointers
  • Classes for internationalisation support

分类二:

Containers: vector, list, stack, ...
Algorithms: find, sort, copy, ...
Iterators are the glue between the two!


The compilation process:

1. Preprocessing - handles meta-information (e.g., #include)
2. Compiling - translates C++ into machine code objects
3. Linking - merges objects into a single application


Google 开源项目风格指南

对语言风格的实践就是最好的学习路径。明白了作者的风格定义思路,也就意味着语言学习达到了优良水准。

路漫漫其修远兮…… 除了坚持,没有捷径。Good luck!