Questions tagged [copy-and-swap]

The copy swap idiom in C++ can be used to simplify the implementation of the assignment operator by used the copy constructor to generate a local copy and swapping it with the current object.

The copy swap idiom in C++ can be used to simplify the implementation of the assignment operator by used the copy constructor to generate a local copy and swapping it with the current object.

Use this tag for questions related to the copy and swap idiom.

For more detail, see also this Stackoverflow question.

66 questions
0
votes
2 answers

why should the parameter of assignment operator be passed by reference in C++?

i read the C++ primer 5th. i saw that it is ok when we use non-reference parameters in assignment operators that use copy and swap,but in the other assignment operators we always use reference parameters and copy the right-hand operand before…
Sherwin
  • 847
  • 1
  • 6
  • 16
0
votes
1 answer

C++ : copy-and-swap idiom, alternative constructor

NB: This question follows a previous one, I hope it is okay to still ask it as a new question. I am trying to implement the "three and a half big rule" (copy-and-swap idiom) for a tree class, which looks like this: class Tree { friend void…
Seub
  • 2,451
  • 4
  • 25
  • 34
-1
votes
1 answer

swap method for a class inherited from enable_shared_from_this<...>

I have implemented a swap method for my class that inherits from enable_shared_from_this<...> class X : public enable_shared_from_this { int x; friend void swap(X& x1, X& x2) { using std::swap; swap(x1.x, x2.x); } …
traveh
  • 2,700
  • 3
  • 27
  • 44
-1
votes
1 answer

Interaction between copy-and-swap idiom and move operations

I'm trying to make a program that implements the interaction between "copy and swap" idiom and and move control operations so I wrote this code: class PInt { public: PInt(int = 0); PInt(const PInt&); PInt(PInt&&) noexcept; PInt&…
Maestro
  • 2,512
  • 9
  • 24
-2
votes
2 answers

Swap elements with php between two html files

I have a problem I'm trying to solve with php, but I'm a total noob in terms of php programming The thing is, I'm modifying an html table with jquery, and what I want to do next is to swap this table with another exact table (exept for the classes…
mathetes
  • 11,766
  • 7
  • 25
  • 32
-3
votes
2 answers

swap and copy idiom can not be used in abstract class

I met a interesting error when I tried to use copy-and-swap idiom in an abstract class. The following code is specially created to demonstrate this situation, if there is anything improper, tell me but don't focus: template class…
1 2 3 4
5