Assignment operator with reference members

后端 未结 4 1591
星月不相逢
星月不相逢 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:48

    What you do is correct, but it is not very exception safe way of writing an copy assignment operator. Also, You should consider using a pointer member rather than an reference member.

    You should implement it using the Copy and Swap Idiom. It has atleast 3 advantages over your implementation.

提交回复
热议问题