在不同环境下,没有被初始化的对象成员的值是多少(原理未知)

在不同环境下,没有被初始化的对象成员的值是多少

#include <iostream>


using namespace std;
class A{
public:
    int a;
    float b;
    double c;
    long d;
    short e;
    void show()
    {
        cout<<"protected:int float double long short:"<<'\n'<<x<<'\n'<<y<<'\n'<<z<<'\n'<<g<<'\n'<<h<<endl;
        cout<<"public:int float double long short:"<<'\n'<<a<<'\n'<<b<<'\n'<<c<<'\n'<<d<<'\n'<<e<<endl;
        cout<<"private:int float double long short:"<<'\n'<<A<<'\n'<<B<<'\n'<<C<<'\n'<<D<<'\n'<<E<<endl;
    }
private:
    int A;
    float B;
    double C;
    long D;
    short E;
protected:
    int x;
    float y;
    double z;
    long g;
    short h;
};

int main(int argc, char *argv[])
{
    A *z=new A();
    z->show();
    cout<<"_________________________"<<endl;
    A Z;
    Z.show();
    return 0;

}


结果1:

protected:int float double long short:

0

0

0

0

0

public:int float double long short:

0

0

0

0

0

private:int float double long short:

0

0

0

0

0

_________________________

protected:int float double long short:

0

5.88247e-39

2.07346e-317

140736512553072 X

0

public:int float double long short:

1619825064 X

4.57986e-41 X

2.12203e-314

140736512552784 X

2869

private:int float double long short:

0

2.8026e-45

2.07406e-317

1

0

发现new方法创建的对象成员初值均为0,另外结果有X的是随着多次编译而发生改变的值

另外我发现,如果屏蔽掉protected的成员那么public成员的值又是不同的规律,屏蔽了以下代码后的结果看结果2。

cout<<"protected:int float double long short:"<<'\n'<<x<<'\n'<<y<<'\n'<<z<<'\n'<<g<<'\n'<<h<<endl;

protected:
    int x;
    float y;
    double z;
    long g;
    short h;

结果2:

public:int float double long short:

0

0

0

0

0

private:int float double long short:

0

0

0

0

0

_________________________

public:int float double long short:

2

0

2.07395e-317

1

0

private:int float double long short:

0

5.88216e-39

2.07346e-317

140734589622736 X

0

对于这样的结果很是不解


猜你喜欢

转载自blog.csdn.net/gghs_up/article/details/62888578