Why do references occupy memory when member of a class?

后端 未结 3 1975
遥遥无期
遥遥无期 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条回答
  •  天涯浪人
    2020-12-21 17:41

    The reference (or pointer) has to be stored in memory somewhere, so why not store it along with the rest of the class?

    Even with your example, the parameter a (int &a) is stored in memory (probably on the stack), then a_ref doesn't use any more memory, it's just an alias, but there is memory used by a.

提交回复
热议问题