← Back to team overview

kicad-developers team mailing list archive

Re: Copy & operator=

 

On 5/26/2016 11:46 AM, Maciej Sumiński wrote:
> On 05/26/2016 04:53 PM, Maciej Sumiński wrote:
>> On 05/26/2016 09:46 AM, jp charras wrote:
>>> Le 26/05/2016 à 08:59, Maciej Sumiński a écrit :
>>>> Thank you Cirilo. I am trying to be really careful here, that is why I
>>>> am asking for more details. I would like to make the code simpler, so
>>>> there are no more doubts about whether to use operator = or Copy() (for
>>>> some classes it is exactly the same code).
>>>>
>>>> For the scenario you described, there is EDA_ITEM::Clone() method.
>>>> Copy() is defined only for a few classes, and others are copied using
>>>> operator=, so it is a bit confusing to me.
>>>>
>>>> Regards,
>>>> Orson
>>>
>>> Usually, Copy() is used when you do not want to copy all members.
>>> Typically when you do not want to change Pback, Pnext, m_Parent, time stamp and a few other pointers
>>> and flags.
>>>
>>> If these members are reinitialized (or have the right value) after copy, using Copy() or = (for
>>> classes which do not define Copy() and can be copied by = ) can give the same result.
>>
>> One more question, for DRAWSEGMENT Copy() is implemented by calling
>> operator=, so it does not transfer the mentioned fields. Should it be
>> the case for every class? How to recognize when a parent, timestamp &
>> list-related fields should be copied as well? They are transferred by
>> automatically generated operator=.
>>
>> Regards,
>> Orson
> 
> Sorry for multiple messages in a row, but I am having more doubts. Some
> of the BOARD_ITEM derived classes have defined copy constructors, and
> others not with a comment that the default copy constructor is fine.
> 
> Classes with copy constructor defined do not copy parents, timestamp or
> list-related fields, so I assume that should be the case for every other
> class. We should pick one option: either we copy everything in copy
> constructors and defined them only for certain classes or every class
> should have a copy constructor that explicitly skips the mentioned
> fields. Any ideas?
> 
> Regards,
> Orson

I don't think there is an easy way to tell you which copy method to use
based on the copy methods defined in a given object.  It all depends on
where and why you are performing an EDA_ITEM copy.  Some copies require
assigning new parents, some require new time stamps, some require both,
and some require neither.  I just depends on where the copy is used.
Can a default copy ctor and assignment operator be used?  I'm sure they
can it's just that you will have to figure out what each IDE_ITEM
dependent version of Copy() does and implement that in the code that
actually does the copying instead of relying on Copy().  I can see
advantages and disadvantages to using the default copy ctor or always
define our own copy ctor for each object.

Cheers,

Wayne



References