小编joh*_*ohn的帖子

混合C和C++时使用nullptr而不是NULL

我有一个非常简单的问题......

我使用的是用C语言编写的SDL API.我正在使用C++.我的编译器支持关键字nullptr,我一直在阅读它.似乎使用而不是使用NULL宏更好.

当我调用SDL_SetVideoMode时,我假设它在失败时返回NULL,所以如果我这样做:

SDL_Surface *test = nullptr;

if ((test = SDL_SetVideoMode(params)) == nullptr)
{
    // to-do code
}
Run Code Online (Sandbox Code Playgroud)

这会准确地检查我在表面测试中的优化是否成功?

c++ sdl nullptr c++11

9
推荐指数
1
解决办法
1253
查看次数

当您多次声明相同的对象/变量时会发生什么(新手)

这样的事情做什么?

static int i;

// wrapped in a big loop
void update_text()
{
    std::stringstream ss; // this gets called again and again
    ++i;
    ss << i;
    text = new_text(ss.str()); // text and new_text are defined elsewhere
    show_text(text); // so is this
}
Run Code Online (Sandbox Code Playgroud)

是用新地址和所有东西在堆栈中创建一个新的ss实例?将sprintf与char数组一起使用会更聪明吗?

c++

0
推荐指数
1
解决办法
3224
查看次数

标签 统计

c++ ×2

c++11 ×1

nullptr ×1

sdl ×1