10 years, 11 months ago.

Default constructor in DigitalIn, DigitalOut, etc

Hi:

mbed source code does not use default constructor (constructor without argument) for DigitalIn, DigitalOut, i.e., you could find:

DigitalIn::DigitalIn()
{
}

in DigitalIn.cpp.

I remember that some compilers may automatic create default constructor for you.

The mbed code for BusIn.cpp could be compiled:

/* 
In BusIn.h:

class BusIn {
    // ...
protected:

    DigitalIn* _pin[16];
};
*/

BusIn::BusIn(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinName p5, PinName p6, PinName p7, PinName p8, PinName p9, PinName p10, PinName p11, PinName p12, PinName p13, PinName p14, PinName p15) {
    PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};

    for (int i=0; i<16; i++) {
        _pin[i] = (pins[i] != NC) ? new DigitalIn(pins[i]) : 0;
    }
}

However, I could not compile mine. What kind of switches that I can use from to turn on default constructor for C++?

Maybe I am missing something, but I don't see a default constructor being used there. Considering settings have to be applied in the constructor a default constructor couldn't really work. What is done in BusIn is dynamically creating DigitalIn objects, similar to: https://mbed.org/questions/1079/Dynamically-reconfiguring-BusOut-and-Bus/

posted by Erik - 25 Jun 2013

1 Answer

HM Yoong
poster
10 years, 11 months ago.

/media/uploads/yoonghm/default_constructor.jpg

keysscan.h:

    DigitalIn     *_pin[16];

_pin[16] is array of DigitalIn but only when arguments to KeysScan constructor is not NC, it will be created. For those uninitialized member of _pin, it it null.

Any comment?

I think you have defined more in your .h file. I did a simple test, and it worked fine. However as soon as you define a DigitalIn in your .h file you will get that message, since it cannot construct that object.

For example my .h file I tested it with:

#include "mbed.h"

class testLib {
  public:
  testLib();
  
  private:
  DigitalIn *_pin[16];
  DigitalIn tralala;
};

That will return an error, because tralala cannot be constructed. But the whole _pin part works fine.

posted by Erik - 25 Jun 2013

My problem solved:

I did this:

class KeysScan : public DigitalIn {
// ...
}

I changed it to

class KeysScan {
// ...
}

Thank

posted by HM Yoong 25 Jun 2013