There are a few things that I almost always do when I put a class together in C++.
1) Virtual Destructor 2) Copy constructor and assignment operator (I either implement them in terms of a private function called Copy(), or declare them private and thus explicitly disallow the compiler to auto generate them).
What things do you find are almost always useful?
Oddly, most of the suggestions here are things I specifically don't do.
I find turning on the gcc flags -Wall
, -Werror
, and (this is the fun one) -Weffc++
help catch a lot of potential problems. From the gcc man page:
-Weffc++ (C++ only) Warn about violations of the following style guidelines from Scott Meyers’ Effective C++ book: · Item 11: Define a copy constructor and an assignment operator for classes with dynamically allocated memory. · Item 12: Prefer initialization to assignment in constructors. · Item 14: Make destructors virtual in base classes. · Item 15: Have "operator=" return a reference to *this. · Item 23: Don’t try to return a reference when you must return an object. and about violations of the following style guidelines from Scott Meyers’ More Effective C++ book: · Item 6: Distinguish between prefix and postfix forms of incre- ment and decrement operators. · Item 7: Never overload "&&", "││", or ",". If you use this option, you should be aware that the standard library headers do not obey all of these guidelines; you can use grep -v to filter out those warnings.
The first i do when putting a class together is putting some doxygen comment above it about why it exists and what it does.
I once worked on a group project where they said they want to document the stuff at the end of the project. And it was all of a mess to put the comments into the code later on. I don't want to have this happen again.
Adding a semicolon after the class definition. This continuously bites me in the ass every time I forget to do it since gcc's error messages are vague and it generally says something like "can't define a type in the return type of a function" which doesn't mean a whole lot...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With