Skip to content

Deriving from TwoWire pointless? #28

@SukkoPera

Description

@SukkoPera

I have tried to use this library. It's an interface library to a MCP23017 IC and I chose it because it allows passing a reference to an arbitrary TwoWire-derived library to be used for i2c communications. Of course the idea was to use it with your SoftwareWire library, as it is the only library of its kind that derives from TwoWire, which is a great idea!

The library saves a reference to the passed object internally. This reference must of course be of type TwoWire&. It is then used every time i2c bus operations are to be performed.

So does all of this work? Well, unfortunately it would be too good to be true :(. The problem is that a lot of the methods that you had to override (let's just consider begin/endTransmission() for instance) are NOT declared virtual in the base TwoWire class. This means that every time they are called through a pointer/reference to the base class (as the MCP23017 library does), the original implementations of the base class themselves get called, instead of your overridden ones.

This is NOT a bug in your library, but likely an oversight (or questionable design decision) by the developers of the original TwoWire, who probably never thought that their class could be subclassed. Nevertheless, this makes the whole point of deriving your class from TwoWire useless. To make something useful out of it, we would need to ask the virtual qualifier to be added to all the relevant methods in the original Wire library.

I think we can try to do this, what's your opinion? This would open a lot of other interesting possibilities, even for other libraries :).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions