1

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?

10
  • did you read the answer?
    – Juraj
    Commented Feb 23, 2019 at 12:28
  • I read it. It does not fully clarify it for me, so I am reluctant to accept it. Commented Feb 28, 2019 at 10:01
  • what is unclear in the answer?
    – Juraj
    Commented Feb 28, 2019 at 10:59
  • I am not so good with programming which makes it difficult for me to understand. Basically I do not understand what "The typedef in BlockDriver.h is only for pointer type, not for instance of the object." means or implies. Even though we are declaring it as a pointer, we later call methods on it. How do those method work when BlockDriver is a BaseBlockDriver. Hope that clarified my confusion. Commented Mar 4, 2019 at 12:50
  • the SdSpiCard class should be derived from the BaseBlockDriver. but for AVR this consumed memory. so the author decided to leave out the base class for AVR. but then everywhere in the code where BaseBlockDriver was used as parameter type, an object of type SdSpiCard could not be used as value. He solved it with creating a macro BlockDriver which is BaseBlockDriver or SdSpiCard
    – Juraj
    Commented Mar 4, 2019 at 13:37

1 Answer 1

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.