首页投稿(暂停使用,暂停投稿)

[cpp deep dive] 初始化initializatio

2016-06-19  本文已影响159人  Quasars

我实在不喜欢纠结于语法,然而这些面试又很喜欢考。(我自己也很好奇到底真相是什么)
[Update 9.10]
====
总结一些关键点:
int *p = new int; - 默认初始化, 随机值.
int *p = new int(); - 值初始化,赋成0.

[Update 9.6]看起来官方一点的说法(常见用法,不严谨)

按照ref, 初始化有n种大类:

stage_0 基本类型(或内建类型 build-in type).

简单粗暴地说就是:

stage_1 类类型的初始化. (类类型成员变量的初始化)

代码片段
//initialization.cc
#include <iostream>
#include <cstdio>
using namespace std;
int x;
float y;
#define ___(str) cout<<"====="<<str<<"===="<<endl;

int make_dirty(){
    double gg=12354.3453765;
    double gg2=12354.3453765;
    double gg3=12354.3453765;
    return 0;
}
/*
int l(){
    return 100;
}
*/

//stage_0
int test1(){

    ___("===global variable ===");
    cout<<x<<endl;//0
    cout<<y<<endl;//0
    

    ___("===local variable default init=====");
    int k;
    double g;
    cout<<k<<endl;//random
    cout<<g<<endl;//random
    

    ___("===local variable explicit user-specified init=====");
    int c = 8;//8,c-style-initial
    //int l();//0
    int ll = int();//0
    int ll2= int(3);//3 /*act like a constructor*/
    cout<<c<<endl;
    //cout<<l<<endl;
    //printf("l -- %p\n", l);
    cout<<ll<<endl;
    cout<<ll2<<endl;
    
    
    ___("===dynamic variable with new_operator=====");
    /*make it dirty at first.*/
    int *tmp=new int[1000];
    for(int i=0;i<1000;i++){
        tmp[i]=(i+0xfecb) % 1009;
    }
    delete[] tmp;
    
    
    int* ll3= new int;//random 
    int* ll4= new int(5);//5
    int* ll5= new int();//0
    cout<<*ll3<<endl;
    cout<<*ll4<<endl;
    cout<<*ll5<<endl;
    return 0;
}

class base1{
public:
    base1(){
        
    }
    base1(int x1) : x(x1) , y(0) , z(10){
        
    } 
    base1(float x1) : z((int)x1), y(z) , x(z){
        
    }
    base1(double x1) : x((int)x1){
        ___("in base1(double)");
        cout<<x<<endl<<y<<endl<<z<<endl;
        y = z = x;
    }
    void f(){
        cout<<x<<endl<<y<<endl<<z<<endl;
    }
private:
    int x;
    int y;
    int z;
};

base1 b2_1((float)11);

void test2(){
    ___("with initialization list");
    base1 b1_2(11);
    b1_2.f();
    
    ___("initialization list's init order");//will be initialized in the declaration order.
    base1 b1_3((float)11);
    b1_3.f();

    ___("without initialization list");
    base1 b1_1;
    b1_1.f();
    
    ___("initialization list not include y&z");
    base1 b1_4(double(11));
    b1_4.f();
    
    ___("global auto-zero-init");
    b2_1.f();
}


int main(){
    cout<<"build-in type"<<endl;
    make_dirty();
    test1();
    
    cout<<"user-defined type(class)"<<endl;
    test2();
}

运行结果.

root@vm1:/home/work/share/CSE274/02_Cpp_Intro# g++ initialization.cc -o test
root@vm1:/home/work/share/CSE274/02_Cpp_Intro# ./test 
build-in type
========global variable =======
0
0
========local variable default init=========
-782730784
4.94066e-323
========local variable explicit user-specified init=========
8
0
3
========dynamic variable with new_operator=========
651
5
0
user-defined type(class)
=====with initialization list====
11
0
10
=====initialization list's init order====
6299808
6299808
11
=====without initialization list====
-782736384
32607
-786215278
=====initialization list not include y&z====
=====in base1(double)====
11
0
-782106642
11
11
11
=====global auto-zero-init====
0
0
11
stage 2 初始化列表2点深入.
#include <iostream>
#include <cstdio>
using namespace std;
#define ___(str) cout<<"====="<<str<<"===="<<endl;
class b{
    b(int x):k(x){}
    int k;
};


class base1{
    base1(){}
private:
    const int cc;
    int& ccr;
    b yhb;
};

int main(){
    //const int x;
    //int &y;
    /*
     *  initialization2.cc: In function ‘int main()’:
        initialization2.cc:11:12: error: uninitialized const ‘x’ [-fpermissive]
          const int x;
                    ^
        initialization2.cc:12:7: error: ‘y’ declared as reference but not initialized
          int &y;

     */
    //base1 bb_1;
     /*
      * root@vm1:/home/work/share/CSE274/02_Cpp_Intro# g++ initialization2.cc -o test
        initialization2.cc: In constructor ‘base1::base1()’:
        initialization2.cc:12:2: error: uninitialized member ‘base1::cc’ with ‘const’ type ‘const int’ [-fpermissive]
          base1(){}
          ^
        initialization2.cc:12:2: error: uninitialized reference member ‘base1::ccr’ [-fpermissive]
        initialization2.cc:12:9: error: no matching function for call to ‘b::b()’
          base1(){}
                 ^
        initialization2.cc:12:9: note: candidates are:
        initialization2.cc:6:2: note: b::b(int)
          b(int x):k(x){}
          ^
        initialization2.cc:6:2: note:   candidate expects 1 argument, 0 provided
        initialization2.cc:5:7: note: b::b(const b&)
         class b{
               ^
        initialization2.cc:5:7: note:   candidate expects 1 argument, 0 provided
        initialization2.cc: In function ‘int main()’:
        initialization2.cc:12:2: error: ‘base1::base1()’ is private
          base1(){}
          ^
        initialization2.cc:31:9: error: within this context
           base1 bb_1;
                 ^

      */
     
    return 0;
}

todo:

上一篇 下一篇

猜你喜欢

热点阅读