Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Vector(const Vector& v, int)
Vector(const Vector& v, int) [message #46839] Fri, 26 August 2016 00:03 Go to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
Hello,

What is a reason to keep the deep copy constructor Vector(const Vector& v, int)?
It prevents automatic generation of default copy constructor for classes using Vector. Default move constructor can be autogenerated, but copy constructor has to be coded manually.

Am I missing something?

TIA


Regards,
Novo
Re: Vector(const Vector& v, int) [message #46841 is a reply to message #46839] Fri, 26 August 2016 17:13 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
With C++11, the reason is to make 'clone' explicit.

We want to prevent this:

Vector<int> a, b;

a = b;

and force the user to write either

a = pick(b);

or

a = clone(b);

(That said, maybe there could be a better way how to achieve this, maybe make copy-construtor explict would work too, but addition int parameter is sort of legacy).

Mirek
Re: Vector(const Vector& v, int) [message #46847 is a reply to message #46841] Fri, 26 August 2016 23:47 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
mirek wrote on Fri, 26 August 2016 11:13
With C++11, the reason is to make 'clone' explicit.
(That said, maybe there could be a better way how to achieve this, maybe make copy-constructor explicit would work too, but addition int parameter is sort of legacy).

Thank you.
Explicit copy-constructor doesn't force to call "clone" for some reason.
Maybe, it makes sense to make pick/clone optional, and let compiler auto-generate default copy constructor? This legacy move semantics doesn't seem to have much sense when compiler already does good job moving temporary data for you. You still will be able to call "pick". Typing code which is not really necessary is very annoying ...

Thanks.


Regards,
Novo
Re: Vector(const Vector& v, int) [message #46848 is a reply to message #46847] Sat, 27 August 2016 06:52 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Novo wrote on Fri, 26 August 2016 23:47

Maybe, it makes sense to make pick/clone optional, and let compiler auto-generate default copy constructor? This legacy move semantics doesn't seem to have much sense when compiler already does good job moving temporary data for you. You still will be able to call "pick". Typing code which is not really necessary is very annoying ...


Well, the idea is to prevent accidental deep copy... In most cases, you want 'pick', but with implicit '=' for deep copy, it is easy to forget.

Mirek
Previous Topic: strcpy issue
Next Topic: HttpRequest issue
Goto Forum:
  


Current Time: Sat Apr 20 06:14:06 CEST 2024

Total time taken to generate the page: 0.02073 seconds