Reference as class member initialization

后端 未结 4 1298
野的像风
野的像风 2020-12-05 10:28

I want to initialize a property of a class that holds a reference to another class by passing such a reference as a parameter to the constructor. However I receive an error:

4条回答
  •  难免孤独
    2020-12-05 11:05

    The error is you're trying to assign through an uninitialized reference: a C++ reference cannot be assigned - the object it refers to is assigned instead - and so, if it's a member, it must be initialized in the initializer list (like the compiler says).

提交回复
热议问题