'C++: Modify a class to make std::swap() work
What should I change in the ScintillaEditView and DocTabView classes so that I'd be able to use std::swap() on those objects?
For example: in Notepad_plus::init().
std::swap(_mainEditView, _subEditView);
std::swap(_mainDocTab, _subDocTab);
Solution 1:[1]
swap has to either have a move or an assignment operator. Your classes ScintillaEditView and DocTabView are very complex classes containing windows and other items that cannot easily be assigned or moved. You could try adding a move operator, but the usual approach here is not to use swap with direct variables, but to use pointers instead. Something like this will probably save you a lot of headaches:
shared_ptr<ScintillaEditView> p_mainEditView, p_subEditView;
// and later
std::swap(p_mainEditView, p_subEditView);
Here you swap pointers to the actual objects. Pointers can easily be assigned and moved, because they don't actually contain resources (like window), but only point to the resources.
By using pointers you can swap without having to modify the original classes at all.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hajo Kirchhoff |
