|
-
January 28th, 2008, 10:43 AM
#4
Re: vector copy using new
 Originally Posted by treuss
I think, a reasonable way of achieving this is to provide a clone() function for your class and then use std::transform.
Code:
class C {
// ...
virtual C* clone() { return new C(*this); }
}
Call to transform should look something like (not tested):
Code:
std::transform(c_vec.begin(), c_vec.end(),
std::back_inserter(new_c_vec),
mem_fun_ref(clone));
or, if you resize new_c_vec before like
Code:
std::transform(c_vec.begin(), c_vec.end(),
new_c_vec.begin(),
mem_fun_ref(clone));
Nice idea! But in this case would it be better just to iterate through c_vec and push_back new Cs instead of creating the clone function? It won't be hard to change the classes that rely on this, but I'm just wondering which option you think is more professional.
Cheers.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|