David,
Thank you for expanding on your statement. I just thought that your original statement was that it was a standard practice in the "Industry".
I myself just don't see the need to ALWAYS do it (from my 16 years of development and 10+ years of C++ programming).
I agree. But also,Quote:
1) When setting policy to get consistent results from a team of programmers a fixed policy which does not involve "judgment calls" is easier to define, implement and maintain.
1) have a policy that you will train your programmers so they know the language,
2) hire competent people who are capable of making intelligent decisions so that you don't need to impose unnecessary policies on programmers.
The above is with a smiley face, but should also be taken to heart.
3) Now you have a policy that gives your programmers a false sense of security. The programmer thinks: "I followed the policy, why think about the actual design".
4) I would tell the programmers to do more up front design to decide what is necessary.
Agreed. Since I can't quantify a word like "almost".Quote:
2) Any class which contains a pointer (almost) always need custom a copy constructor and assignment operator. Those which want to have the same pointer value should be explicitly documented in any case.
Don't forget the assignment operator.Quote:
3) Any class which does internal reference counting, needs a copy constructor.
I quess I would look at the flip side. If there was no reason to have a copy constructor in the first place, and you require people to write one, you are lengthening your development cycle unnecessarily. Your clients would probably be happier if you could deliver quicker and more reliably.Quote:
4) A class that today does not "need" a copy constructor will often be "upgraded" to contain a new data member that requires a copy constructor. As a (contrived example), consider a class with starts with 5 data members all built-in non-pointer types. This does not need a copy constructor or assignment operator to behave correctly. Over time additional members are added, they are all either build in types or well behaved classes, this goes on until there are 50 data members. THEN IT HAPPENS, a pointer data member is required. At this point the copy constructor and assignment operator are needed. The programmer now has to write the code for alll 51 members!!!!. Much better to amortize the cost (time) since day 1. It is hard to justify (to boss or client) why addding one "little pointer" requires a week of testing to insure that all 50 of the other members are being properly copied!!!.
Well, if it was just for debugging, I would suggest putting the stuff in #if blocks.Quote:
5) Some classes which consist of just built in types and well behaved classes STILL require a (user written)copy constructor. Consider a simple object that counts the number of times a certain method has been called FOR THAT INSTANCE. This is often the type of item added for debugging and code coverage testing. Should a copy constructor be written (again having to implement and test ALL of the members) when this is added and then deleted when this is removed????
Again, hire competent people. I would agree with your statement but instead of "empty", I would tell them to declare them as private and not implement them.Quote:
6) Many programmers never intend for their objects to be copied or assigned. They just dont think about it. If you REQUIRE them to write these functions, they must think. Either write a valid public method, or write an empty private method (explicitly stating that "you cant to this").
I disagree, if there is no reason to allow a default constructor to be called, declare the default constructor as private and don't implement it.Quote:
Default Constructor : All members should ALWAYS be initialized so this is required.
I agree. But you don't mention the case where equality testing isn't even valid.Quote:
Equality operator : A great many classes contain private data members that are not pertinent to equality testing [lazy evaluation patterns, item #5 above, etc.)
I disagree.Quote:
In short liefe is EASIER for everybody (in the long run) if these routines are ALWAYS implemented.
Regards,
John Flegert
