Assignment operator with reference members

后端 未结 4 1596
星月不相逢
星月不相逢 2020-12-01 09:04

Is this a valid way to create an assignment operator with members that are references?

#include 

struct A
{
    int &ref;
    A(int &Ref)         


        
4条回答
  •  星月不相逢
    2020-12-01 09:41

    What you do its technically correct as far as I know, but it generates trouble. For instance, consider what happens with a derived class from A, since its assignment operator generates a new object (slicing). Can't you just turn the reference into a pointer within your class?

    Besides that, copy constructors and assignment operators usually take its argument by const&.

提交回复
热议问题