What does {0} mean when initializing an object?

后端 未结 10 744
-上瘾入骨i
-上瘾入骨i 2020-11-29 14:46

When {0} is used to initialize an object, what does it mean? I can\'t find any references to {0} anywhere, and because of the curly braces Google s

10条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 15:29

    It's syntatic sugar to initialize your entire structure to empty/zero/null values.

    Long version

    SHELLEXECUTEINFO sexi;
    sexi.cbSize = 0;
    sexi.fMask = 0;
    sexi.hwnd = NULL;
    sexi.lpVerb = NULL;
    sexi.lpFile = NULL;
    sexi.lpParameters = NULL;
    sexi.lpDirectory = NULL;
    sexi.nShow = nShow;
    sexi.hInstApp = 0;
    sexi.lpIDList = NULL;
    sexi.lpClass = NULL;
    sexi.hkeyClass = 0;
    sexi.dwHotKey = 0;
    sexi.hMonitor = 0;
    sexi.hProcess = 0;
    

    Short version

    SHELLEXECUTEINFO sexi = {0};
    

    Wasn't that much easier?

    It's also nice because:

    • you don't have to hunt down every member and initialize it
    • you don't have to worry that you might not initialize new members when they're added later
    • you don't have have to call ZeroMemory

提交回复
热议问题