zero-initialization

Why doesn't initializing a C++ struct to `= {0}` set all of its members to 0?

好久不见. 提交于 2020-05-11 10:56:27
问题 After doing a ton of testing and writing this answer (note: the downvote on it was BEFORE my total rewrite of it), I can't understand why = {0} doesn't set all members of the struct to zero! If you do this: struct data_t { int num1 = 100; int num2 = -100; int num3; int num4 = 150; }; data_t d3 = {0}; printf("d3.num1 = %i\nd3.num2 = %i\nd3.num3 = %i\nd3.num4 = %i\n\n", d3.num1, d3.num2, d3.num3, d3.num4); ...the output is: d3.num1 = 0 d3.num2 = -100 d3.num3 = 0 d3.num4 = 150 ...although I

C++ Zero-Initialization

跟風遠走 提交于 2019-12-03 14:56:57
问题 I'm having trouble understanding when and why exactly a member in my class is zero-initialized according to http://en.cppreference.com/w/cpp/language/zero_initialization. Consider the following test program: #include <iostream> #include <stdio.h> class MyTest { private: const static unsigned int dimension = 8; void (* myFunctions [dimension])(); public: MyTest() {} void print() { for(unsigned int i=0; i < MyTest::dimension; i++) { printf("myFunctions[%d] = %p\n", i, this->myFunctions[i]); } }

C++ Zero-Initialization

蓝咒 提交于 2019-12-03 04:41:35
I'm having trouble understanding when and why exactly a member in my class is zero-initialized according to http://en.cppreference.com/w/cpp/language/zero_initialization . Consider the following test program: #include <iostream> #include <stdio.h> class MyTest { private: const static unsigned int dimension = 8; void (* myFunctions [dimension])(); public: MyTest() {} void print() { for(unsigned int i=0; i < MyTest::dimension; i++) { printf("myFunctions[%d] = %p\n", i, this->myFunctions[i]); } } }; int main() { //We declare and initialize an object on the stack MyTest testObj = {}; testObj.print