Inheriting from SdFat/Sd2Card

The hardware I'm working with has two micro-controllers with a single SD card shared between them. (arranged as such SPIMaster->SPISlave->SDCard ) What I'm trying to do is make it so that I can run anything on the Master with normal SdFat library code and have it transparently work across to the card on the other microcontroller. To do this I'm deriving my own class from Sd2Card and using it to proxy function calls from the Master to an instance of the real Sd2Card instance that's running on the Slave through my own existing command structure. This all works perfectly so far if I manually create my own (derived)SdCard, SdVolume, and SdFile instances, but what I really want to do is be able to create an SdFat object.

And that's where my problem comes in. The SdCard object in the SdFat instance is a private member, so I can't just visualize it and put a special implementation in a class derived from SdFat without having to make a new private member and changing all of the methods that access it. I really want to avoid modifying the library code if it's at all possible but right now the only options I can think of are changing the SdCard member to a pointer and making new constructors (more invasive) or templating it (the better option?)

Is there a better way to do this?

Would changing the private members of SdFat to protected help? Then your derived classes could access the SdFat protected members.

That's a minimal mod to SdFat.

I developed SdFat without a lot of design. It started as personal project to log some data to SD cards and then evolved. I didn't think about people deriving classes from it.

Now it has a lot of users so changing the structure is hard. It is amazing what impacts existing software.

That would be a perfect solution, but I can't seem to get it working the way I expect it to. my simple test case:

class UtilityFat:public SdFat {
    UtilityFat( )  {   };
    void set( UtilityCard& card) {

        card.init(1,5); // calls the derived init
        card_.init(1,5);// calls the base init
        card_ = card;
        card_.init(1,5);// calls the base init

The reference passes into the function fine, but it's either not replacing the card_ object, or it's calling thru to the base method on the derived class. I've got more reading to do.

Looks like an ugly problem. If card_ is a member of SdFat with type Sd2Card, card_.init() will call Sd2Card::init().

This statement won't change what member functions get called.

card_ = card;

It will copy values into the member variables of SdFat::card_.

card_ is not a pointer so you don't change what gets called.

Sorry if my explanation is not too clear.

No, that's basically what I figured was happening, but I've still got options.