I am trying to write object oriented code for Arduino.
Being more used to higher level languages, I cant find the correct syntax to do what I want.
Here is how I would instinctly write it :
#include "Instrument.h"
Instrument::Instrument(Debounce& piano_key){
_piano_key = &piano_key;//assign the adress of piano_key to the value of _piano_key
}
bool Instrument::read(){
return _piano_key->read();
}
By default, you cannot use C++'s operator new to create instances at runtime. (You only have 1KB or 2KB of RAM, so that's probably a good thing.) However, since the libraries do support C's malloc(), you can enable operator new yourself.
If you use operator new, you must use operator delete, but not for the worry that you state, exactly.
Yes, as you say, you should always match the same heap manager routines, choosing compatible allocation and deallocation together. The malloc()/realloc() go with free(), for example.
Also, new goes with delete, but these aren't functions, they're operators. In C++, you need to use the official operator new and operator delete so that constructors and destructors get called appropriately. It's okay to define the operators to use the malloc/free scheme as I show above, and in fact, that's what 99% of C++ compilers would do, until they want to do fancy things to integrate with leak detectors or other debugging tools.