Published on: 2025-07-06 23:27:10
C++ pop quiz time: what are the values of a.a and b.b on the last line in main of this program? #include <iostream> struct foo { foo () = default ; int a ; }; struct bar { bar (); int b ; }; bar :: bar () = default ; int main () { foo a {}; bar b {}; std :: cout << a . a << ' ' << b . b ; } The answer is that a.a is 0 and b.b is indeterminate, so reading it is undefined behaviour. Why? Because initialization in C++ is bonkers. Default-, value-, and zero-initialization Before we get into the
Keywords: constructor default initialization initialized zero
Find related items on AmazonGo K’awiil is a project by nerdhub.co that curates technology news from a variety of trusted sources. We built this site because, although news aggregation is incredibly useful, many platforms are cluttered with intrusive ads and heavy JavaScript that can make mobile browsing a hassle. By hand-selecting our favorite tech news outlets, we’ve created a cleaner, more mobile-friendly experience.
Your privacy is important to us. Go K’awiil does not use analytics tools such as Facebook Pixel or Google Analytics. The only tracking occurs through affiliate links to amazon.com, which are tagged with our Amazon affiliate code, helping us earn a small commission.
We are not currently offering ad space. However, if you’re interested in advertising with us, please get in touch at [email protected] and we’ll be happy to review your submission.