SD library and implementation inheritance.

Hi,

I've been trying to write a library that will have all the features of the 'File' class but with a few extra functions for manipulating files. Based on my limited understanding I have attempted to base my new class on the 'File' class using this syntax for my class definition....

class FileFunctions:public File
{
  public:

From what I've read, I though this should mean that my 'FileFunctions' class will automatically inherit methods like flush() and close() without the need for me to actually type the code in.

So far so good, my new library compiles OK, uploads Ok and the test sketch executes without visible errors. However, when I check the contents of my SD card, I discovered that the file I've created does exist but is empty.

Having played with arduinos with SD cards, I know this typically means that the open() has worked ok, but the close() hasn't. I have checked my sketch, and after writing each line of data to the file I flush() and close() it.

Since I haven't explicitly coded flush() and close() I expected these methods to be inherited from the parent class. However, in this case it doesn't seem to work. (to clarify - my class has inherited close() and flush() but they don't actually do anything! To prove this, I manually added these two functions to my class....

void FileFunctions::flush(void)
{
  _myFile.flush();
}

void FileFunctions::close(void)
{
  _myFile.close();
}

This time the library works, and data gets written to the file.

Have I missed something, maybe I have misunderstood inheritance? If I'm wrong, what's the point of inheritance if I've got to type in a link to the underlying function anyhow?

Thanks

FileFunctions is a File...

class FileFunctions:public File

FileFunctions uses _myFile...

void FileFunctions::flush(void)
{
  _myFile.flush();
}

Which is it? Does FileFunctions use a _myFile object (whatever that is) or is FileFunctions a subclass of File?

Read this. It will help... http://snippets-r-us.com/

Ok, problem solved (mostly). I've consulted my local IT guru (my wife) and discovered that there are different types of inheritance, some of which don't allow the new class to make the inherited method public.

In the SD.h file, the File class is defined. It has:

  virtual size_t write(uint8_t);
  virtual size_t write(const uint8_t *buf, size_t size);
  virtual int read();
  virtual int peek();
  virtual int available();
  virtual void flush();

That they are virtual means that a derived class MUST implement them.