代码之家  ›  专栏  ›  技术社区  ›  sharptooth

C++中初始化的原始类型是什么?

  •  31
  • sharptooth  · 技术社区  · 14 年前

    使用初始化列表时:

    struct Struct {
        Struct() : memberVariable() {}
        int memberVariable;
    };
    

    int bool , float , enum ,指针)成员变量为 默认初始化 . 它得到的值是实现定义的还是所有实现都是相同的?

    6 回复  |  直到 14 年前
        1
  •  40
  •   Johannes Schaub - litb    14 年前

    你说的不对。对象不是默认初始化的,而是 值已初始化 . 它的价值是明确的

    int = 0, 
    bool = false, 
    float = 0.0f,  
    enum = (enum type)0, 
    pointer = null pointer
    pointer to member = null member pointer
    

        2
  •  32
  •   Community Ramakrishna.p    4 年前

    标准上说( 8.5/5 )

    如果T是非POD类类型(第9条),则调用T的默认构造函数(如果没有可访问的默认构造函数,则初始化是错误的);

    如果T是数组类型,则每个元素都默认初始化;

    否则, 对象已初始化为零 .

    初始化T类型对象的值意味着:

    如果T是具有用户声明的构造函数(12.1)的类类型(第9条),则调用T的默认构造函数(如果T没有可访问的默认构造函数,则初始化是错误的);

    如果T是数组类型,则每个元素都初始化为值;

    否则,

    .

    它得到的值是实现定义的还是所有实现都是相同的?

    因此,所有实现的值都是相同的。

    Struct

     Struct *a =new Struct; // default initialization
    
     //memberVariable will be initialized to 0 because if T is a non-POD class type
     //the default constructor for T is called 
    
     Struct *b = new Struct(); //value initializes Struct, which calls the default ctor.
    
     //memberVariable will be initialized to 0 in this case also.
    

    编辑 :

    正如@Johannes注意到的,基本类型(int,bool,float,enum,pointer)成员变量是 value-initialized default initialized .

        3
  •  11
  •   Bart van Ingen Schenau    14 年前

    对于基本类型, 意味着对象被初始化为0、0.0或NULL(视情况而定)。

    以上的内容对C++ 98是有效的。在C++ 03中,术语被重新定义了一点。现在,使用 () (这在语法上只适用于成员对象)导致 ,这对于基元类型意味着存储适当的值0、0.0或NULL。

        4
  •  4
  •   Jon Hanna    14 年前

    0

    如果你打电话 () 在原语上,效果与指定默认值(如果原语是静态的话)相同。

        5
  •  0
  •   Ganeshkumar    3 年前

    具有自动和动态存储持续时间的非类变量的默认初始化会生成具有不确定值的对象(静态和线程本地对象的初始化为零)

    参见下面的示例

    #include <string>
     
    struct T1 { int mem; };
     
    struct T2
    {
        int mem;
        T2() { } // "mem" is not in the initializer list
    };
     
    int n; // static non-class, a two-phase initialization is done:
           // 1) zero initialization initializes n to zero
           // 2) default initialization does nothing, leaving n being zero
     
    int main()
    {
        int n;            // non-class, the value is indeterminate
        std::string s;    // class, calls default ctor, the value is "" (empty string)
        std::string a[2]; // array, default-initializes the elements, the value is {"", ""}
    //  int& r;           // error: a reference
    //  const int n;      // error: a const non-class
    //  const T1 t1;      // error: const class with implicit default ctor
        T1 t1;            // class, calls implicit default ctor
        const T2 t2;      // const class, calls the user-provided default ctor
                          // t2.mem is default-initialized (to indeterminate value)
    }
    
        6
  •  -1
  •   yesraaj    14 年前

    这取决于你如何实例化一个类,如果你使用ClassName(),POD类被默认初始化为零,对于非POD类,默认构造函数被调用,但是如果你使用ClassName,没有括号,默认初始化不会发生。

        7
  •  -2
  •   reko_t    14 年前

    像int这样的原生类型通常会得到 garbage value 不管发生了什么,它都会驻留在创建它的内存区域中。但是,这在标准中没有定义,它也可能被初始化为0,这在调试构建中非常常见。