I am developing some HW drivers, which access registers. I have a few classes, f
ID: 658908 • Letter: I
Question
I am developing some HW drivers, which access registers. I have a few classes, for example.
ClassRegisterBlock1, ClassRegisterBlock2.
In each of these classes, they define the bits to manipulate, the register offsets etc and validate the parameters. However all the functions use the same way ofaccessing the HW, via a memory map and then there are common functions like setRegisterBit.
I was planning on putting the common functions, like setRegisterBit, CreateMemoryMap etc.. into a base class and then the ClassRegisterBlock(x) inherits from that.
Is this a sensible use of base class? as strictly speaking the ClassRegisterBlock is not a kind of RegisterFunctions class. Is there a better way of doing that?
I am using c++.
Explanation / Answer
Here are my two cents.
Inheritance should be carefully used. We have IS A and HAS A relationship checks for this. Reusability should not be only criteria for using inheritance.
There are other ways we can use for code reusability. You can have class that represents your data ie. ClassRegisterBlock, and then your RegisterFunctions class can be simple Utility class that takes ClassRegisterBlock object as parameter and operate on that. Then you can have these common H/W related functionality there.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.