To use an uninitialized object of a built-in type with automatic storage time, undefined behavior is used. Of course, I highly recommend always initializing member variables of the built-in type inside the class. Despite this, I assume that a member of the built-in type without an initializer is always initialized to zero if the corresponding object of the class type has a static storage duration (i.e., a Global object). My assumption is that the full memory of an object of type class with a static storage duration is reset to zero.
Example:
#include <iostream> using namespace std; class Foo { public: int bar; }; Foo a; int main() { Foo b; cout << "a.bar " << a.bar << "\n"; cout << "b.bar " << b.bar << "\n"; return 0; }
Compile:
$ g++ -o init init.cpp -Wall -pedantic # gcc 7.2.1 init.cpp: In function 'int main()': init.cpp:14:31: warning: 'b.Foo::bar' may be used uninitialized in this function [-Wmaybe-uninitialized] cout << "b.bar " << b.bar << "\n"; ^~~~
GCC only complains about a member, an object of type class with a duration of automatic storage of b.bar, not a.bar. So, am I right?
Feel free to change the name of this question.
thanks
Peter source share