为什么将对象初始化为空 [英] Why initialize an object to empty

查看:26
本文介绍了为什么将对象初始化为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 MSDN 的帮助下学习 windows 编程.为什么有人会像下面这样初始化一个对象?

I am learning windows programming with the help of MSDN.Why would somebody initialize an object like the following?

WNDCLASS wc = { };

这会清零对象的所有内存吗?整个源代码如下:

Will this zero all the memory of the object? Whole source code is following:

#ifndef UNICODE
#define UNICODE
#endif 

#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    // Register the window class.
    const wchar_t CLASS_NAME[]  = L"Sample Window Class";
    WNDCLASS wc = { };

    wc.lpfnWndProc   = WindowProc;
    wc.hInstance     = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    // Create the window.

    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        L"Learn to Program Windows",    // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
        );

    if (hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);

    // Run the message loop.

    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);

            FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));

            EndPaint(hwnd, &ps);
        }
        return 0;

    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

推荐答案

大概是为了利用自动初始化.

Presumably to take advantage of Automatic Initialization.

参考:

C++03 标准 8.5.1 聚合
第 7 段:

如果列表中的初始值设定项少于聚合中的成员数,则每个未显式初始化的成员都应值初始化(8.5).[示例:

If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be value-initialized (8.5). [Example:

 struct S { int a; char* b; int c; };
 S ss = { 1, "asdf" };

1 初始化 ss.a,用 "asdf" 初始化 ss.bss.c 带有 int() 形式的表达式的值,即 0.]

initializes ss.a with 1, ss.b with "asdf", and ss.c with the value of an expression of the form int(), that is,0. ]

虽然值初始化定义在,
C++03 8.5 初始化器
第 5 段:

While Value Initialization is defined in,
C++03 8.5 Initializers
Para 5:

对类型 T 的对象进行值初始化意味着:
— 如果 T 是具有用户声明的构造函数 (12.1) 的类类型(第 9 节),则调用 T 的默认构造函数(如果 T 没有可访问的默认构造函数);
— 如果 T 是没有用户声明的构造函数的非联合类类型,则每个非静态T 的数据成员和基类组件是值初始化的;
— 如果 T 是数组类型,那么每个元素都是值初始化的;
— 否则,对象被零初始化

To value-initialize an object of type T means:
— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized

这篇关于为什么将对象初始化为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆