This is more of a design question.
Currently I have multiple classes (in different projects) which have different roles but there is one common method which performs the same duty. I was thinking about having a base class for all theses classes so each of them could inherit this class and implement this method to save duplication.
My question is should I have one base class for all the classes which are in multiple projects, or should I have one base class per project?
Thanks,
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance. The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by destructors.
Explanation: For the implementation of multiple inheritance, there must be at least 3 classes in a program. At least 2 base classes and one class to inherit those two classes. If lesser, it becomes single level inheritance.
A class can be derived from more than one base class. In a multiple-inheritance model (where classes are derived from more than one base class), the base classes are specified using the base-list grammar element.
The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class.
I would define an interface that contains that one shared method. I would then put that interface in it's own project. Then have you concrete implementations reference that project and implement the interface.
Something like this..
In general 1 class for all your projects is a lot better than creating the same class for each project.
Actually I dare to say that in almost any case, reusing the same code, rather than copying it. Always is better
In general static libraries are the best tool to solve the problem you are currently facing.
EDIT:
If you are working in visual studio i would solve this as follows. Create a project containing the base class declaration and definition. This projects should be of the type static library.
The other projects should include the directory containing the header file in inclusion dirs. And the directory containing the lib file in the additional dependency directories.
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