C ++ Trivia (1)

FUNC () is equivalent to func (void)

That in C ++, the parameter list is empty means that does not accept any parameters. The reason to pay attention to this because in C language, the parameter list is empty means that parameter uncertainty . Both semantics is a huge difference as we learn C to learn C ++ for people who need to pay attention a little.

void pointer can not be implicitly converted to other types of pointer

Need static_cast<T*>syntax explicit conversion, is also inconsistent with the syntax of C, C language is allowed voidpointer assignment directly to other types of pointers. Examples of a best embodiment of the difference between the two is on the C and C ++ NULLmacro definitions different:

#undef NULL
#ifdef __cplusplus
    // C++
    #define NULL 0
#else
    // C
    #define NULL ((void *)0)
#endif

char signed char is not necessarily equal to

In fact, C ++ standard does not specify charthe type of symbol, depending on the particular implementation, and that various other integer types. So to use charthe type of small integer arithmetic to be the best time to clearly specify its symbol.

const global variables for the current default scope cpp file

In order to definitions in the header file constglobal variables, C ++ provisions constglobal variables with internal linkage default properties, to extend its scope to the whole project, you need to manually add the definition of the externkeyword:

// 这两行全局变量定义等价
const int MAX = 233;
static cosnt int MAX = 233;

This does not have to put on a few keywords when you create a struct union enum objects

In the C language is the need to take appropriate keywords, and several types in C ++ classtypes, the type name can just write:

struct Foo {
    int count;
};

// ok
Foo foo;

Processing parameter is ignored

Sometimes the need to deal with some of the parameters passed in a function call, but if the parameter is not placed with the matter may even cause compiler warnings error, this time on the need to handle manually specify ignore parameters:

// 方法一
void func(int n, int m) {
    // 忽略参数m
    (void)m;

    std::cout << n << std::endl;
}

// 方法二
void func(int n, int) {
    std::cout << n << std::endl;
}

Guess you like

Origin www.cnblogs.com/HachikoT/p/12159105.html