C++ in Arduino : auto initialization of private variable at constructor

In C++, there may be cases where we need to initialize a private variable with a particular value. In this post, I want to show how initialization should be done in the right manner so that compiler can generate the exact binary instruction & save us from any undefined runtime issue. We will first go through the basics and then implement the concept for initializing an LCD-type object acting as an LCD driver. 

let's say you have a class Counter with a variable noOfCounts


now you might be thinking to initialize the noOfCounts variable like this 

Counter::Counter()
{
   noOfCounts = 0;
}

although it works BUT it is not the preferred approach. There is a better way which is : 

Counter::Counter():noOfCounts(0)
{}

This way the member noOfCounts gets initialized before the constructor even starts to execute. this is important because this is also the only way to initialize const member data & references.

The above argument is true if you want to initialize an object (which is also a variable) with arguments.  

Now let's see how you actually create an LCD object in your Arduino sketch. You may have declared object like this 
#include <Arduino.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);      //object created with pin numbers as argument

But Now let's say you want to make a custom class of your own that internally uses the LiquidCrystal class and does LCD operations for example showing an animation that the actual 
LiquidCrystal does not do. If you ask me then I would think about the following ways. 
  1. Modify the original LiquidCrystal Class        
  2. Make a new class  by Inheriting LiquidCrystal class or 
  3. Make your own Class where a private variable is of LiquidCrystal type and then initialize that object at your own custom class constructor 
I am going to show you solution no 3 (which is an Aggregate pattern) keeping the original Library as it is. 

First of all, Create your CustomLCD class and declare a LiquidCrystal type object lcd as a private variable :

class CustomLCD
{
   public:
          CustomLCD();
          void showSomething(void);
   private:
          LiquidCrystal lcd;
};

Then you can auto init this lcd variable like the way we discussed avobe i.e. using the colon : at the constructor

CustomLCD::CustomLCD():lcd(12, 11, 5, 4, 3, 2)   //here private variable lcd object gets auto initilized with pins                                               
{
  lcd.begin(16, 2);
}

Then you can create a CustomLCD kind object in your sketch and use it

#include "CustomLCD.h"

CustomLCD myCustomLCD;   //CustomLCD object is created

void setup() 
{
  Serial.begin(9600);
  myCustomLCD.showSomething();
}

void loop() 
{}

here is the circuit simulation output in proteus:



Note: you can also init any basic type class variable in this way & it is a good practice to prevent abnormal runtime behaviors. In a later post, I will explain why! 

All the Source Code and circuit diagrams are available at this link https://github.com/hassin23ayz/ArduinoCpp/tree/master/T36_auto_init_constructor 

0 comments:

Post a Comment

Categories

Pages

Firmware Engineer

My photo
Works on Firmware, Embedded Linux, Smart Metering, RTOS, IoT backend

Contact Form

Name

Email *

Message *

Copyrighted by Hassin. Powered by Blogger.