官术网_书友最值得收藏!

Pointing to a null pointer

Another new feature in modern C++ is a keyword named nullptr that replaces the NULL macro to represent a null pointer. Now, there's no ambiguity in the use of the NULL macro for zero numeric or a null pointer. Let's suppose we have the following two method's signature in our declaration:

    void funct(const char *);
void funct(int)

The former function will pass a pointer as the argument and the latter will pass the integer number as its argument. Then, we invoke the funct() method and pass the NULL macro as the parameter, as shown here:

    funct(NULL);

What we intend to call is the former function. However, since we pass the NULL parameters, which is basically defined as 0, the latter function will be invoked. In modern C++, we can use the nullptr keyword to ensure that we will pass a null pointer to the argument. The invocation of the funct() method should be as follows:

    funct(nullptr);

Now the compiler will invoke the former function since it passes a null pointer to the argument, and this is what we expect. There will be no ambiguity anymore, and it will avoid unnecessary future problems.

主站蜘蛛池模板: 通州市| 滁州市| 依兰县| 深州市| 茂名市| 台江县| 葫芦岛市| 都江堰市| 桃园市| 四子王旗| 镇江市| 田东县| 水富县| 平塘县| 鄯善县| 彰化市| 房山区| 崇礼县| 绥化市| 肃宁县| 房山区| 乌兰县| 边坝县| 大荔县| 江陵县| 岚皋县| 怀化市| 亳州市| 巴青县| 黄山市| 台东市| 内乡县| 东城区| 尉氏县| 康乐县| 东乡县| 荔波县| 成安县| 东阿县| 通海县| 信宜市|