Why must const members be initialized in the constructor initializer rather than in its body?

后端 未结 3 474
后悔当初
后悔当初 2020-11-30 21:04

Why must class members declared as const be initialized in the constructor initializer list rather than in the constructor body?

What is the difference

3条回答
  •  北海茫月
    2020-11-30 21:43

    Because constant variables and references must be initialized at time of declaration i.e before use. But Constructors will assign value to a varaible not initailize the variable therefore you must use initailizier list for constant and references

提交回复
热议问题