相关疑难解决方法(0)

什么是nullptr?

我们现在拥有许多新功能的C++ 11.一个有趣且令人困惑的(至少对我来说)是新的nullptr.

嗯,不再需要令人讨厌的宏了NULL.

int* x = nullptr;
myclass* obj = nullptr;
Run Code Online (Sandbox Code Playgroud)

尽管如此,我还没有得到如何nullptr运作.例如,维基百科的文章说:

C++ 11通过引入一个new 关键字作为一个区分空指针常量来解决这个问题:nullptr.它的类型为nullptr_t,它是可隐式转换的,可与任何指针类型或指向成员类型的类型相媲美.除了bool之外,它不可隐式转换或与整数类型相比.

它是一个关键字和一个类型的实例?

另外,你有另一个例子(在维基百科旁边)哪里nullptr优于好老0

c++ pointers nullptr c++11

546
推荐指数
8
解决办法
31万
查看次数

为什么std :: cout输出在发送NULL后完全消失

我花了一段时间才弄清楚为什么一些cout输出似乎消失在以太中.罪魁祸首:

std::cout<< "This line shows up just fine" << std::endl;
const char* some_string = a_function_that_returns_null();
if (some_string == 0)
    std::cout<< "Let's check the value of some_string: " << some_string << std::endl;

std::cout<< "This line and any cout output afterwards will not show up" << std::endl;
Run Code Online (Sandbox Code Playgroud)

上面代码段的输出将是:

This line shows up just fine
Let's check the value of some_string:     
Run Code Online (Sandbox Code Playgroud)

因此将NULL输入cout将禁用所有输出.为什么?以及如何解决它?

这不会一直发生 - 具有相同代码的同事获得所有预期的输出.如果你想知道为什么我不能阻止使用if语句将NULL输入cout:我在大型代码库中工作,并且不知道这会发生在哪里!我所知道的是我从未出现过的cout陈述.


更多信息:

a_function_that_returns_null()实际上是getenv("HOST").我检查了命令行echo $HOST,因为HOST变量是空的.如果我这样做export HOST=(bash风味),输出就在那里.我不知道HOST变量最初包含什么,也不知道getenv在修改HOST变量之前最初返回的内容; 我所知道的(some_string == 0)都是真的.

c++ gcc

21
推荐指数
2
解决办法
7129
查看次数

无法在屏幕上打印nullptr的值

我正在阅读关于nullptrg ++和VS2010的锻炼和锻炼.

我什么时候做的

#include <iostream>
using namespace std;

auto main(void)->int
{
    int j{};    
    int* q{};   

    cout << "Value of j: " << j << endl; // prints 0
    cout << nullptr << endl;
    cout << "Value of q: " << q << endl; // prints 0

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

打印nullptr屏幕上的值,g ++和VS给出了编译错误.是否不允许打印nullptr屏幕上的值?

c++ c++11

8
推荐指数
2
解决办法
2586
查看次数

标签 统计

c++ ×3

c++11 ×2

gcc ×1

nullptr ×1

pointers ×1