1. Implement all the default functions provided by the compiler like Constructor, Copy Constructor, Destructor, Assignment Operator and Address-of Operator to avoid compiler defining its own
Just define it even if you think you don't need it at the moment, your class will be Scalable if you define all this functions and avoid any nasty surprises for your users, if they use any of this functions without knowing that they are not at all implemented.
Incase, you don't want to provide implementation for any of them, just provide an empty function for future use.
Copy-Constructor
You will avoid issues related to shallow copy if you provide your own Copy Constructor.
Constructor
Use initialization list instead of assignment while initializing data members. This is mandatory for Consts and References but you can use initialization list for all the data as it is more efficient then assignment.
Assignment Operator
1. Check for self assignment
2. Delete previous data before assignment
3. Return object reference to allow chains of assignments
4. Assign values to all data members
Destructor
Declare destructors as virtual in your base classes
2. If you want to make your class behave just like a interface for your more specific derived classes, you can provide atleast one pure virtual function, so that your class will become abstract class and you will not be able to create object of it.
3. Make all your data as private. Constructor, Destructor and any other functions as public.
Declare your Constructor as private if you want to make your class as final or not-inheritable.
4. Prefer pass-by-reference to pass-by-value
No comments:
Post a Comment