i reading "effective c++ scott meyers", in item 11 recommending use "copy , swap" technique inside assignment operator:
widget& widget::operator=(const widget &rhs) { widget temp(rhs); // copy constructor swap(temp); //swap *this return *this; }
but in item 12 written:
it makes no sense have copy assignment operator call copy constructor.
i think item 11 , item 12 contradictory. understanding incorrectly?
in 2 citations of "effective c++ scott meyers" mention 2 different aspects discussed.
- in code fragment in item 11 scott shows 1 of ways implement assignment operator.
- in item 12 citation mention deals avoiding code duplication between assignment operator , copy constructor. 1 paragraph above citation says:
the 2 copying functions have similar bodies, , may tempt try avoid code duplication having 1 function call other.
my understanding of part of item 12 this: if try write below (to have copy assignment operator call copy constructor) wrong:
prioritycustomer::prioritycustomer(const prioritycustomer& rhs) : customer(rhs), // invoke base class copy ctor priority(rhs.priority) { logcall("prioritycustomer copy constructor"); } prioritycustomer& prioritycustomer::operator=(const prioritycustomer& rhs) { logcall("prioritycustomer copy assignment operator"); this->prioritycustomer(rhs); // line wrong!!! return *this; }
Comments
Post a Comment