This is most probably more of a C++ question than an Arduino question; but since this is an Arduino library, I am asking here.
I am using the SdFat library for my project. The library defines a BlockDriver
object in one of its header file which only declares the typedef
of BaseDriver
to some other classes.
#if ENABLE_EXTENDED_TRANSFER_CLASS || ENABLE_SDIO_CLASS
typedef BaseBlockDriver BlockDriver;
#else // ENABLE_EXTENDED_TRANSFER_CLASS || ENABLE_SDIO_CLASS
typedef SdSpiCard BlockDriver;
#endif // ENABLE_EXTENDED_TRANSFER_CLASS || ENABLE_SDIO_CLASS
#endif // BlockDriver_h
typedef SdSpiCard BlockDriver;
makes sense to me. But if ENABLE_EXTENDED_TRANSFER_CLASS
or ENABLE_SDIO_CLASS
are defined, BlockDriver
is typedeffed into BaseBlockDriver
. BaseBlockDriver
is an abstract base class (as defined here). An instance of the BlockDriver
object is later used directly in the FatVolume
class here.
My question is, how does BlockDriver
's method get defined when it abstract base class BaseBlockDriver
is typedeffed to BlockDriver
?
1 Answer 1
The typedef in BlockDriver.h is only for pointer type, not for instance of the object. The object is always of type SdSpiCard
, but in one case it is not an implementation of BlockDriverBase, so the function parameters can't refer it by base type.
#if ENABLE_EXTENDED_TRANSFER_CLASS || ENABLE_SDIO_CLASS
class SdSpiCard : public BaseBlockDriver {
#else // ENABLE_EXTENDED_TRANSFER_CLASS || ENABLE_SDIO_CLASS
class SdSpiCard {
use of BlockDriver typedef
class FatFileSystem : public FatVolume {
public:
bool begin(BlockDriver* blockDev, uint8_t part = 0) {
I was curious why the version without BaseBlockDriver. I asked the author Bill Greiman and he answered:
This is due to history. Long ago using a virtual base class cost lots of RAM and flash so I did this mainly for 328 boards
BlockDriver
is aBaseBlockDriver
. Hope that clarified my confusion.