Home » U++ Library support » U++ Core » Transfer Semantics - Initializer list and copy constructors
Re: Transfer Semantics - Initializer list and copy constructors [message #11887 is a reply to message #11884] |
Sun, 30 September 2007 08:32  |
 |
mirek
Messages: 14267 Registered: November 2005
|
Ultimate Member |
|
|
forlano wrote on Sat, 29 September 2007 18:27 |
luzr wrote on Tue, 10 July 2007 22:11 | The simple rule: Do what you need. If you get crash because of broken pick semantics (the ASSERT in Vector with items = -1), use "deep copy variant" (<<= instead of =, constructor with additional int parameter (e.g. 1).
|
Hello,
for first time I needed to copy the content of a vector in another vector WITHOUT to destroy the original vector. This is my class:
class Person : Moveable<Person> {
int currentProposer;
int cursor;
public:
Index<int> preference;
int RankProposer(int k);
int GetNext(); int GetCurrentRank(); int GetCurrentProposer();
int GetProposedTo();
int FindPosition(int k);
void AcceptProposal(int k);
String ToString() const { String s; for (int i=0; i<preference.GetCount();i++) s<<AsString(preference[i]) + ' '; return s; }
Person() {currentProposer = 0; cursor = -1; }
};
Then I declared two vectors:
Vector<Person> M, R;
After some operations made with M I would like to copy M in R. I used
R<<=M;
But the program crashed. Which is the correct syntax for this task?
Thanks,
Luigi
|
Unfortunately, this case is a little bit more complicated, you also have to provide deep copy operations for Person (which, frankly, is a tedious process, but there is no other way).
At minimum provide Person(const Person&, int) constructor that creates deep copy, and use DeepCopyOption template as base class to generate the rest.
Search
http://www.ultimatepp.org/srcdoc$Core$pick_$en-us.html
for "DeepCopyOption".
Mirek
|
|
|
Goto Forum:
Current Time: Sun Aug 03 13:55:50 CEST 2025
Total time taken to generate the page: 0.06932 seconds
|