I am trying to understand the following instantiation of bno, in the main routine of Arduino.
Adafruit_BNO055 bno = Adafruit_BNO055();
further down bno is then used.
bno.setExtCrystalUse(true);
In the pertaining Adafruit_BNO055h. file, the class Adafruit_BNO055 is created. With Class Adfruit_BNO055_Sensor being the base class, defined in an other .h file
class Adafruit_BNO055 : public Adafruit_Sensor {
public:
And the constructor is created in the Adafruit_BNO055.cpp
What I dont understand, is: shouldnt it read Adafruit_BNO055 bno; to create an instance of the class? The book says: Type Adafruit_BNO055, the class name, and the variable, bno, in this case.
Why is the constructor there? reading Adafruit_BNO055 bno = Adafruit_BNO055();
You can have several constructors, depending on the number of arguments. So
Adafruit_BNO055 bno = Adafruit_BNO055();
will call a different constructor, which you haven't found yet.
The .h file that declares the classes and methods may also contain the implementations for the simpler methods. A constructor with no arguments may just call the "real" constructor by passing it the defaults for each of those arguments. That's a 1-line function so it may be implemented in the .h file.
Arduino uses C++, so this is a pedantic distinction.
johnguy:
What I dont understand, is: shouldnt it read
Adafruit_BNO055 bno;
to create an instance of the class? The book says: Type Adafruit_BNO055, the class name, and the variable, bno, in this case.
Why is the constructor there? reading
Adafruit_BNO055 bno = Adafruit_BNO055();
Both of those methods of construction will give the same result. When you don't use an explicit constructor, the compiler will select the default constructor (the one that needs no arguments). If you don't have a default constructor, you'll get an error trying to use that first snippet.
The .cpp file contains the implementation code of the class, but the .h file contains its definition. It's just as important as the .cpp file, especially in this case. You are correct to notice that the constructor in the .cpp requires arguments. What you miss is this line in the .h file:
The .h file defines default values that will automatically be assigned to those arguments if you choose to not specify them. In this case, all the arguments have default values, so you don't need to specify them if the defaults are what you need.
The constant BNO055_ADDRESS_A is also defined in the .h file.