Why can't I make a vector of references?

前端 未结 9 610
梦如初夏
梦如初夏 2020-11-22 03:32

When I do this:

std::vector hello;

Everything works great. However, when I make it a vector of references instead:



        
9条回答
  •  自闭症患者
    2020-11-22 03:58

    By their very nature, references can only be set at the time they are created; i.e., the following two lines have very different effects:

    int & A = B;   // makes A an alias for B
    A = C;         // assigns value of C to B.
    

    Futher, this is illegal:

    int & D;       // must be set to a int variable.
    

    However, when you create a vector, there is no way to assign values to it's items at creation. You are essentially just making a whole bunch of the last example.

提交回复
热议问题