Assignment operator with reference members

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

    Another solution is to use the reference_wrapper class ( in functional header ) :

    struct A
    {
        A(int& a) : a_(a) {}
        A(const A& a) : a_(a.a_) {}
    
        A& operator=(const A& a)
        {
            a_ = a.a_;
            return *this;
        }
    
        void inc() const
        {
            ++a_;
        }
    
        std::reference_wrappera_;
    
    };
    

提交回复
热议问题