c++ - What does {0} mean when initializing an object? -
when {0}
used initialize object, mean? can't find references {0}
anywhere, , because of curly braces google searches not helpful.
example code:
shellexecuteinfo sexi = {0}; // 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); }
without it, above code crash on runtime.
what's happening here called aggregate initialization. here (abbreviated) definition of aggregate section 8.5.1 of iso spec:
an aggregate array or class no user-declared constructors, no private or protected non-static data members, no base classes, , no virtual functions.
now, using {0}
initialize aggregate trick 0
entire thing. because when using aggregate initialization you don't have specify members , spec requires unspecified members default initialized, means set 0
simple types.
here relevant quote spec:
if there fewer initializers in list there members in aggregate, each member not explicitly initialized shall default-initialized. example:
struct s { int a; char* b; int c; }; s ss = { 1, "asdf" };
initializes
ss.a
1
,ss.b
"asdf"
, ,ss.c
value of expression of formint()
, is,0
.
you can find complete spec on topic here
Comments
Post a Comment