@nrailgun
2016-01-06T14:10:53.000000Z
字数 2632
阅读 1766
程序设计
好久没有用 C++,完全忘记了,抓紧看看 C++ primer 和 Effective C++。虽然很讨厌 C++,然而 C++ 还是很有用的。
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.
It is class template, not template class. For example, std::vector<Dtype>
is a class template.
Just like size_t
and container::size_type
, you should use container::difference_type
instead of ptrdiff_t
. But it's use int
.
Instead of size_type
.
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 result is machine depedent. On x86, integer division round result down to the nearest integer. Remainder is caulated like this:
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.
WTF.cc:
++i = 10; // works, i = 10.
C++ guarantees deleting null pointer to be safe. Well, not for me.
return_type function_name(type (&)[ARRAY_LENGTH]);
You might define a function pointer like this:
return_type (*foo) (...);
typedef
function pointer is cool:
typedef return_type (*func_type) (...);
void foo( bool (int, int) );
works, but a better idea is:
void foo( bool (*)(int, int) );
Default argument can be different in base class and derived class, and it's bounded to cause troubles.
Otherwise default constructor will be called. This leaves thing looks so wired.
Makes little sense.
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.
But using smart_ptr
could be a better idea.
typename
tell compiler to treat Container::size_type
as type.
template <typename Container>
void foo()
{
typename Container::size_type sz;
}
template <typename T, size_t N>
T sum(const T (&a)[N]);
Instantiation will be implied.
template <typename T>
T foo(T);
int (*p)(int) = foo;
The order base constructed is decided according to the base class list.
WTF. 2 base class objects share one base class object. I can smell danger near.