0

I came across a class which sole purpose was to create blinking leds. He used the class for 2 objects which had a different blinking interval. I started thinking in how I could use constants but nothing came to me.

Is it possible to use a const int in combination with the constructor to use different constants for objects?

I am aware that a const int is to be initialized at the same place where it is declared, so I'd guess that the answer is 'no'

If not, are there workarounds to achieve the same thing? That is using different constants (so less memory usage) for different objects of the same type.

The used platform in question is the arduino IDE and an atmega328P. May it perhaps be that the compiler recognizes that the variables are actually used as constants and treat them as such?

.h

class   BlinkingLed
{
private:
    int   blPin;
    short blinkInterval; // <-- the contant
    bool  blinking;
    bool  ledOn;
    long  lastTime;

public:
    BlinkingLed(int, int);
    void setBlinkInterval(int); // never used, should not exist
    int  getBlinkInterval();    // never used, should not exist
    void setLedOn(bool);
    bool getLedOn();
    void attachPin();
    void heartBeat();
};

.cpp

BlinkingLed::BlinkingLed(int aPin, int aBlinkInterval) // constructor
{
    blPin = aPin;
    blinking = false;
    ledOn = false;
    blinkInterval = aBlinkInterval; // <-- the contant
}

The objects are made with this line. The aki class needs 2 BlinkingLed objects.

AKI aki(new BlinkingLed(23,333), new BlinkingLed(22,667), 24); // Red blinks 90/minute, green 45/minute

This is the constructor of aki:

AKI::AKI(BlinkingLed *aRedLight, BlinkingLed *aGreenLight, int aBellPin)
{
    redLight = aRedLight;
    greenLight = aGreenLight;
    bellPin = aBellPin;
}

The 333 and 367 are stored in variables and I want these to become constants to preserve memory space. How do I do that?

  • 6
    *"Is it possible to use a const int in combination with the constructor "* Yes, `struct A {const int x; A(int x) : x(x) {}};` Is this what you want? – HolyBlackCat May 07 '20 at 07:51
  • 2
    Please provide a [mcve], the question is rather unclear. – 463035818_is_not_an_ai May 07 '20 at 07:53
  • 2
    "*I am aware that a const int is to be initialized at the same place where it is declared*" That's correct, but not for class members. Otherwise, `const` class members would be useless pre-C++11. This is one of the reasons [member initializer list](https://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor) should be used. – Yksisarvinen May 07 '20 at 07:56
  • I have added code – bas knippels May 07 '20 at 08:12
  • you have the answer in the first comment – Juraj May 07 '20 at 08:14
  • you have added code, but it is not clear what is the problem with the code, there is also no constant in the code... do you want to make the member `blinkInterval` `const` but when you do you get an errror? – 463035818_is_not_an_ai May 07 '20 at 08:19
  • The first comment is not clear enough for me, I am sorry – bas knippels May 07 '20 at 08:21
  • It is clear. 2 others do understand what the problem is. The problem is that `blinkInterval' is a variable and not a constant. That means that it is using more memory than is needed. I am asking here, how I can turn it into a constant. With as goal to preserve memory. There are 2 objects made with this class. The the blinkInterval is different for the the objects. I will add more code – bas knippels May 07 '20 at 08:27
  • use fields initialize part of constructor implementation to initialize constants or references. `BlinkingLed::BlinkingLed(int aPin, int aBlinkInterval) : blPin(aPin), blinkInterval(aBlinkInterval) {` – Juraj May 07 '20 at 09:44

1 Answers1

2

To summarize the discussion, and correct some of your wrong assumptions:

class   BlinkingLed {
private:
    const byte   blPin;  // This should definitely be const
    const unsigned short blinkInterval; // <-- the desired constant (why?)
    bool  blinking;
    bool  ledOn;
    unsigned long  lastTime;

public:
    BlinkingLed(byte pin, unsigned short interval) 
       : blPin(pin), blinkInterval(interval)  {}

    // void setBlinkInterval(int); // impossible for a const Interval

    void init() {pinMode(blPin, OUTPUT);}  // should not be done in a c'tor
    void run(); // to be called as often as possible for a smooth heartbeat

    unsigned short  getBlinkInterval() {return blinkInterval;}  // why not
    void setLed(bool); // stops blinking
    bool isLedOn();
    void heartBeat();  // start blinking
};

BTW: data type int usually blames you for not thinking about it. :)

datafiddler
  • 1,755
  • 3
  • 17
  • 30
  • Thank you. I know that int shouldnt be used because it can differ per platform. Again this code is not creation. I do not understand the comment behind the init() what exactly is a c'tor? – bask185 May 07 '20 at 19:55
  • c'tor is short for constructor. A piece of code that is performed _very_ early for global objects. For tasks that should run in setup(), you normally define an init() method. – datafiddler May 08 '20 at 14:25
  • I know what a constructor is, but not that people called it "c'tor". But tnx – bas knippels May 10 '20 at 07:11