Code:
#include <iostream>
using namespace std;
class Example
{
public:
int _public;
friend ostream& operator<< (ostream& stream, Example& o);
protected:
int _protected;
private:
int _private;
};
ostream& operator<< (ostream& stream, Example& o) {
stream <<
"_public=" << o._public << endl <<
"_protected=" << o._protected << endl <<
"_private=" << o._private << endl;
return stream;
}
int main(int argc, char const *argv[])
{
Example e;
cout << e << endl;
return 0;
}
Output
_public=4196960
_protected=0
_private=4196368
Question:
All the three members are uninitialized. But only the public
and private
members have garbage values in them. Why is the protected
member initialized to zero? Is there a reason for that?
g++ version and flags
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
-std=c++11