相关疑难解决方法(0)

初始化对象时{0}意味着什么?

何时{0}用于初始化对象,这是什么意思?我找不到任何{0}地方的任何引用,并且由于大括号,谷歌搜索没有帮助.

示例代码:

SHELLEXECUTEINFO sexi = {0}; // what does this do?
sexi.cbSize = sizeof(SHELLEXECUTEINFO);
sexi.hwnd = NULL;
sexi.fMask = SEE_MASK_NOCLOSEPROCESS;
sexi.lpFile = lpFile.c_str();
sexi.lpParameters = args;
sexi.nShow = nShow;

if(ShellExecuteEx(&sexi))
{
    DWORD wait = WaitForSingleObject(sexi.hProcess, INFINITE);
    if(wait == WAIT_OBJECT_0)
        GetExitCodeProcess(sexi.hProcess, &returnCode);
}
Run Code Online (Sandbox Code Playgroud)

没有它,上面的代码将在运行时崩溃.

c c++

250
推荐指数
6
解决办法
6万
查看次数

如何在C++中初始化嵌套结构?

我在程序中创建了几个不同的结构.我现在有一个嵌套结构的结构,但我无法弄清楚如何正确初始化它们.结构如下所列.

/***POINT STRUCTURE***/
struct Point{
    float x;                    //x coord of point
    float y;                    //y coord of point
};

/***Bounding Box STRUCTURE***/
struct BoundingBox{
    Point ymax, ymin, xmax, xmin;
};

/***PLAYER STRUCTURE***/
struct Player{
    vector<float> x;            //players xcoords
    vector<float> y;            //players ycoords
    BoundingBox box;
    float red,green,blue;       //red, green, blue colour values
    float r_leg, l_leg;         //velocity of players right and left legs
    int poly[3];                //number of points per polygon (3 polygons)
    bool up,down;               
};
Run Code Online (Sandbox Code Playgroud)

然后我尝试初始化一个名为player的新创建的Player结构.

//Creates player, usings vectors copy and iterator …
Run Code Online (Sandbox Code Playgroud)

c++ nested structure initialization data-structures

10
推荐指数
1
解决办法
2万
查看次数

C++ empty-paren成员初始化 - 将内存清零?

我最初写了一些像这样的代码:

class Foo
{
public:
  Foo() : m_buffer()
    {}

private:
  char   m_buffer[1024];
};
Run Code Online (Sandbox Code Playgroud)

比我更聪明的人说使用m_buffer()初始化程序会将内存清零.我的意图是保持记忆不被初始化.我没有时间进一步讨论它,但它激起了我的好奇心.

以前,我曾认为总是在初始化列表中列出每个成员是明智的.

有人可以进一步描述这种行为吗?

1)为什么empty-paren初始化程序会填充内存?

2)它只适用于POD数据类型吗?我听说是这样,但没有标准的方便.

谢谢

c++ standards initialization

3
推荐指数
1
解决办法
1275
查看次数

标签 统计

c++ ×3

initialization ×2

c ×1

data-structures ×1

nested ×1

standards ×1

structure ×1