7

Possible Duplicates:
What is the copy-and-swap idiom?
Copy constructor and = operator overload in C++: is a common function possible?

Is there a way that I can make the body of the copy constructor and assignment operator contain the same code without actually having duplicate code (except for the function headers)?

Community
  • 1
  • 1
Matt Munson
  • 2,903
  • 5
  • 33
  • 52
  • Sure: http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom – GManNickG Feb 22 '11 at 00:58
  • 1
    Not a duplicate. The other question is the answer to this one. – Edward Strange Feb 22 '11 at 01:13
  • 2
    @Crazy: That's the point of duplicates. It's not "is the question the same" it's "does the other question solve the same problem". How would you answer this question? By repeating the information contained in another one, and that would be wasteful. – GManNickG Feb 22 '11 at 01:28
  • This is not a duplicate. If anything the answer to "What is the copy-and-swap idiom?" should be here. – FreelanceConsultant Aug 25 '15 at 23:09

3 Answers3

4

One common way is the copy-and-swap idiom. You would have to implement a swap operation, but if done correctly, you have the additional benefit of having exception safe assignment.

Community
  • 1
  • 1
Pablo
  • 8,644
  • 2
  • 39
  • 29
0

Create a function

    init(various parameters you need){
...
//common initializing process
}

then call this function from all you constructors, copy, and assignments operators

Jason Rogers
  • 19,194
  • 27
  • 79
  • 112
-2

Place the functionality in a separate method and then call that from both your copy constructor and assignment operator code.

Alternatively, you could just call your assignment operator from the copy constructor.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • 2
    Both methods stop the copy-constructor from initializing (rather, it assigns), and neither is exception-safe. – GManNickG Feb 22 '11 at 00:59
  • @GMan: ?? The copy constructor must initialize itself to a known state first. I'm not sure I understand your objection if it then calls the assignment operator. – Jonathan Wood Feb 22 '11 at 01:05
  • @Jonathan: Consider if one of the members cannot be default-constructed. – GManNickG Feb 22 '11 at 01:12
  • I can see how using the copy swap method would be the preferable choice for heavy duty programming. But I think Jonathan's solution is nice as a straightforward lightweight approach, and also captures the essence of how the problem is tackled in general. – Matt Munson Feb 22 '11 at 01:26
  • 2
    @Matt: Copy-and-swap is not exactly "heavy-duty". If you find that to be too much, perhaps C++ isn't the right language for you. – GManNickG Feb 22 '11 at 01:28
  • 2
    ....and he picks the worst answer available. – Edward Strange Feb 22 '11 at 01:37
  • Well, I up voted all the answers because I thought they were all good. As a beginner programmer, whose main interest is in other areas, I take learning this stuff one step at a time, as I need the info. I know such an approach is probably frowned upon, but that is the reality of my situation, as someone who can only devote so much time to mastering the craft. – Matt Munson Feb 22 '11 at 01:52
  • @GMan: The percent of cases where it's impossible to set everything to a known state before calling an assignment operator is so small that your focus with it seems suspect. @Crazy Eddie: There are three answers, two are the same. How could one of the two be the worst? @Matt: Don't mind people on SO who seem like they have to prove something. There are some jerks but there are also people who are just trying to help. – Jonathan Wood Feb 22 '11 at 02:33
  • 1
    @Jonathan: The point is that you're supposing we use a potentially unusable and conceptually incorrect solution over another equally simple but absolutely correct solution. And thanks for calling me a jerk. – GManNickG Feb 22 '11 at 03:44
  • 1
    @Matt: What reality entails you use a sub-par solution over the best-practice solution, when both require the same amount of effort? You'd be better off just going for the best known solution. – GManNickG Feb 22 '11 at 03:44
  • 1
    @GMan: In this case, the "jerk" comment was more apt to describe Crazy Eddie. I don't have a problem with people trying to show how a particular approach is better. But there's quite a few people, seemingly increasing lately, on SO who seem more fixated with putting others down. – Jonathan Wood Feb 22 '11 at 04:03
  • @GMan I can't say the exact difference in effort required, maybe none once I fully understand how to implement copy-and-swap. But I do know that the explanation of swapping that was linked to was orders of magnitude longer than the above explanation by Jon, and by skimming it I could see that it would take me a while to derive the information. But I do appreciate your info, and I bookmarked the page. I'm sure I'll get around to reading it, if I progress that far. – Matt Munson Feb 22 '11 at 06:52