Why do references occupy memory when member of a class?

后端 未结 3 1977
遥遥无期
遥遥无期 2020-12-21 16:56

I have been told that references, when they are data members of classes, they occupy memory since they will be transformed into constant pointers by the compiler. Why is tha

3条回答
  •  旧时难觅i
    2020-12-21 17:38

    Imagine that a class is just a user defined data type. You need to have something which can lead you to the actual thing that you are referencing. Using the actual value in the second case is more about the compiler and his work to optimize your code. A reference should be an alias to some variable and why should this alias use memory when it could be optimized to be taken directly from the stack.

提交回复
热议问题