[关闭]
@nrailgun 2016-01-06T14:10:53.000000Z 字数 2632 阅读 1766

C++ Primer & Effective C++ 阅读笔记

程序设计


好久没有用 C++,完全忘记了,抓紧看看 C++ primerEffective C++。虽然很讨厌 C++,然而 C++ 还是很有用的。

size_t and container::size_type

In C, you should use size_t for indexing and counting (most of the time int works fine). As for C++ STL container, using container::size_type is a better idea.

Class template

It is class template, not template class. For example, std::vector<Dtype> is a class template.

ptrdiff_t and container::difference_type

Just like size_t and container::size_type, you should use container::difference_type instead of ptrdiff_t. But it's use int.

bitset::count() returns size_t

Instead of size_type.

Array & Pointer Syntax

Syntax Mean
type [][] array of arraies
type *[] array of pointers
type (*)[] pointer to array
typedef type typename[] typedef array
typedef type *typename typedef pointer

Devision & Remainder

Devision result is machine depedent. On x86, integer division round result down to the nearest integer. Remainder is caulated like this:

remainder=aab×b.

The result thus can be a little confusing.

Associative

Binay operators are left associative, while assignment operators are right associative. Associativities are determined even in C. Most unary operators are right associative, while most binary operators are left associative.

++i is left value

WTF.cc:

  1. ++i = 10; // works, i = 10.

Deleting NULL pointer

C++ guarantees deleting null pointer to be safe. Well, not for me.

Pass array to function by reference

  1. return_type function_name(type (&)[ARRAY_LENGTH]);

Function Pointer

You might define a function pointer like this:

  1. return_type (*foo) (...);

typedef function pointer is cool:

  1. typedef return_type (*func_type) (...);

Function argument

  1. void foo( bool (int, int) );

works, but a better idea is:

  1. void foo( bool (*)(int, int) );

Virtual function default argument

Default argument can be different in base class and derived class, and it's bounded to cause troubles.

Derived copy constructor should call base copy constructor

Otherwise default constructor will be called. This leaves thing looks so wired.

Virtual operator = is confusing

Makes little sense.

Virtual functions in C++ constructor and destructor

If virtual functions are called in constructor or destructor, since derived class are not constructed yet or destructed already, base class version function are called. This is more reasonable than Java.

std::map key class type needs default contructor

But using smart_ptr could be a better idea.

typename in template

typename tell compiler to treat Container::size_type as type.

  1. template <typename Container>
  2. void foo()
  3. {
  4. typename Container::size_type sz;
  5. }

non-type template argument

  1. template <typename T, size_t N>
  2. T sum(const T (&a)[N]);

Assign function template to function pointer

Instantiation will be implied.

  1. template <typename T>
  2. T foo(T);
  3. int (*p)(int) = foo;

Multiple inheritance construct order

The order base constructed is decided according to the base class list.

Virtual inheritance

WTF. 2 base class objects share one base class object. I can smell danger near.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注