Does anyone know the reason why the variables have to be defined at the top of function

前端 未结 5 785
遥遥无期
遥遥无期 2021-02-09 14:18

I have a question, does anyone know why the variables have to be defined initialized at the beginning of a function? Why can\'t you initialize or define variables in the

5条回答
  •  無奈伤痛
    2021-02-09 15:09

    In C++ and C99, you can define variables in the middle of a function. What you cannot do is refer to a variable that has not yet been defined.

    From the point of view of object-oriented programming, it wouldn't make much sense otherwise: By defining a variable you bring an object to life (by calling its constructor). Before that, there is no object, so there's nothing to interact with until you pass the point of object construction.

提交回复
热议问题