davidhood2
davidhood2

Reputation: 1367

Default Constructor - deferring initialisation of member variables

I am looking to create a default constructor for my object in C++, which, when called, simply calls another constructor but with fixed values.

I have been looking at similar problems: Are default constructors called automatically for member variables? and Explicitly defaulted constructors and initialisation of member variables Which indicate, that when a default constructor is called, the default constructors of the member variables, unless specified, are also called.

However, the issue I have is that the member variables (from the ARMmbed library) I am using, do not have default constructors - hence this is not working.

Is there a way to "delay" this issue, because, in the constructor called by the default constructor, all these member variables are allocated to and it all works out - is there a way of letting the compiler know this?

Thanks very much!

The header and implementation code I am using is below!

class Motor: public PwmOut
{
public:
    //Constructor of 2 pins, and initial enable value
    Motor(); //Default constructor
    Motor(PinName dutyPin, PinName enable_pin, bool enable);  
private:
    bool enable; //Boolean value of enable
    DigitalOut enablePin; //Digital out of enable value
};

Implementation:

/**
* Default constructor
**/
Motor::Motor() //I don't want to initialise member variables here
{
    this = Motor::Motor(p23,p24,true); //As they are initialised in this constructor anyway?
}
/**
* Constructor for Motor class. Takes 1 PwmOut pin for PwmOut base class and 1 pin for DigitalOut enable
**/
Motor::Motor(PinName dutyPin, PinName enable_pin, bool enable):
    PwmOut(dutyPin), enablePin(enable_pin)
{
    //Logic in here - don't really want to duplicate to default constructor
}

Upvotes: 1

Views: 686

Answers (1)

Praetorian
Praetorian

Reputation: 109159

You can use C++11's delegating constructors feature for this.

Motor::Motor()
: Motor(p23,p24,true)
{}

If your compiler does not support that then you'll have to initialize the data members in the mem-initializer list, and move the logic that you do not want repeated to another function.

Motor::Motor()
: PwmOut(p23), enablePin(p24), enable(true)
{
    Init();
}

Motor::Motor(PinName dutyPin, PinName enable_pin, bool enable):
    PwmOut(dutyPin), enablePin(enable_pin), enable(enable)
{
    Init();
}

Motor::Init()
{
  // Move the initialization logic in here
}

Another option, as Alf mentions in the comments, is to introduce a base class that you can delegate construction to.

class MotorBase
{
public:
    MotorBase(PinName enable_pin, bool enable)
    : enable(enable), enablePin(enable_pin)
    {
       // initialization logic goes in here
    }
private:
    bool enable; //Boolean value of enable
    DigitalOut enablePin; //Digital out of enable value
};

class Motor : public PwmOut, MotorBase
{
public:
    Motor(); //Default constructor
    Motor(PinName dutyPin, PinName enable_pin, bool enable);
};

Motor::Motor()
: PwmOut(p23), MotorBase(p24, true)
{}

Motor::Motor(PinName dutyPin, PinName enable_pin, bool enable):
    PwmOut(dutyPin), MotorBase(enable_pin, enable)
{}

Upvotes: 2

Related Questions