0

Possible Duplicate:
What is The Rule of Three?

Hi, I've been reading about the topic, and many websites tell me about why do I need a ctor, copy ctor, and a dtor. But I have had trouble finding good examples on how to use them. Also I don't really get why we need to pass parameters to the copy ctor as a const. Any help would be appreciated!

Community
  • 1
  • 1
Jota
  • 234
  • 2
  • 11
  • 1
    These websites are all wrong, by the way. The *rule of three* does not concern the constructor; on the other hand, it includes the assignment operator. – Konrad Rudolph Mar 07 '11 at 17:20
  • You actually don't have to pass the parameter to your copy constructor as const. I used to work for someone who used non-const references. It's really stupid, but you can do it. – Edward Strange Mar 07 '11 at 17:30
  • @Konrad Rudolph: I know what you mean but I would argue that when the rule of three is being applied you do need to make sure the constructor is there as well. You do not want to rely on the user of your class correctly using value initialization to make sure pointers are NULL (thus I think of it as the rule of 4 (all four compiler generated methods better by overridden when you have owned RAW pointers)). – Martin York Mar 07 '11 at 20:19
  • @Martin I would go further: every class needs a custom constructor. That C++ even provides a default constructor is stupid. But this is irrelevant here: the rule of three (not four, three) doesn’t concern the constructor and it is a very restrictive rule: it is only there to provide the *bare minimum* of correctness guarantee, nothing more. You are right that a *meaningful* class also needs to provide a constructor. – Konrad Rudolph Mar 07 '11 at 20:21

1 Answers1

1

I think this What is The Rule of Three? may answer your question.

Community
  • 1
  • 1
ultifinitus
  • 1,813
  • 2
  • 19
  • 32