-3

Trying to develop an understanding of the functioning of the the SDFat library for Arduino IDE. I stumbled across these lines of codes in the SDfat.h header file.

/**
 1* \class SdBaseFile
 2* \brief Class for backward compatibility.
 3*/
 4 class SdBaseFile : public FatFile {
 5 public:
 6 SdBaseFile() {}
 7 /**  Create a file object and open it in the current working directory.
 8  *
 9  * \param[in] path A path for a file to be opened.
 10  *
 11 * \param[in] oflag Values for \a oflag are constructed by a
 12  * bitwise-inclusive OR of open flags. see
 13  * FatFile::open(FatFile*, const char*, uint8_t).
 14  */
 15 SdBaseFile(const char* path, uint8_t oflag) : FatFile(path, oflag) {}
 16 };

If someone could please explain me how this class declaration works.

1) What does the :public Fatfile in line#4 do.

2) Why are there two constructors in line#6 and line#15 (if at all they are that).

My comprehension is being limited by the lack of syntactical understanding of the definition/ declaration here. Appreciate the help.

Thanks

Vada Poché
  • 763
  • 5
  • 16
sherinkapotein
  • 47
  • 2
  • 10
  • 3
    1. It means that this class is derived from another class named FatFile. 2. The class can be instantiated in two ways, so there's two constructors. I suggest that you read a good book on C++ – Vada Poché Feb 05 '17 at 07:58
  • 2
    Any C++ tutorial/book would answer these questions in detail. 1) Inheritance. 2) Yes, they are constructors, the first is a default constructor, the other is a constructor that takes input to open a file – Remy Lebeau Feb 05 '17 at 07:59
  • 1
    Read up on inheritance and function overloading. For book recommendations, see [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Greg Kikola Feb 05 '17 at 07:59

3 Answers3

2
  1. :public Fatfile means that the class SdBaseFile inherits from the class FatFile
  2. A class may have multiple constructors (this is not only C++, but most object-oriented languages, as far as I know). When you use the class SdBaseFile you can choose any of these constructors.
Wagner Patriota
  • 5,494
  • 26
  • 49
2
  1. :public Fatfile means that SdBaseFile publically inherits from Fatfile. This is actually called an is-a relationship i.e SdBaseFile is-a Fatfile. You can use a Fatfile * to point to objects of SdBaseFile.

  2. There are two type of constructors, one does not take any arguments, and the other one takes two arguments, those two arguments are in fact passed to the base class from which this class SdBaseFile has been derived.

Rishi
  • 1,387
  • 10
  • 14
1

keep in mind the 'SdBaseFile() {}' is the default constructor. This is always defined in a class and usually when a framework doesn't want the default constructor called it will be a private constructor.

the other constructor seems to be what you want to used. As the default constructor doesn't seem to do anything other than construct an class with no file associated with it. My impression of this would change if I knew what was in the FatFile class.

Robert Baron
  • 214
  • 1
  • 9
  • 1
    The default constructor is not always defined: when omitted, the compiler will only provide a default constructor for you if you do not define any other constructors. – Greg Kikola Feb 05 '17 at 08:29