Let me start by saying my programming background is in c# (.net), and I am pretty new to C++ and arduino.
I am creating a library, and it is pretty involved, so I am going to try to post a very, very simplified version to ask a question to a problem I am having
Lets say I have a library, call it MyLib, which has the constructors:
MyLib(unsigned short id _id)
{
//do something with the _id
}
MyLib()
{
//default to an ID and some other things
}
Now, lets say I have a second library, lets call it SuperLib which uses MyLib, but also has it's own constructor('s), this is pseudo code
#include "MyLib.h"
MyLib aLib;
MyLib bLib;
SuperLib(unsigned short int aID, unsigned short int bID)
{
aLib=MyLib(aID);
bLib=MyLib(bID);
}
Now, in a sketch, I have something like this, more pseudo code:
#include <MyLib.h>
#include <SuperLib.h>
SuperLib aSuperLib(1,2);
SuperLib bSuperLib(3,4);
void setup()
{
Serial.begin(9600);
Serial.println(aSuperLib.DoSomething());
Serial.println(bSuperLib.DoSomething());
}
void loop()
{
//some cool stuff
}
What is happening is I am getting the exact same results for both aSuperLib.DoSomething, and for bSuperLib.DoSomething, and the results should definitely be different based on having different ID's.
I am pretty sure it is because they are both using the same chunk of memory, but I am not quite sure how to handle this.
I have tried a few things, of which none worked.
First thing I tried was in Arduino sketch:
SuperLib* aSuperLib = new SuperLib(1,2);
SuperLib* bSuperLib = new SuperLib(3,4);
//rest of sketch the same
I also tried doing something similar in my SuperLib.h where I declare MyLib variables.
Neither worked.
I THINK, this is a pointer issue, in that I have to properly allocate the memory, but I cannot figure this out, can anyone help me out here?
Sorry for the LONG post, just wanted to make sure I explain the problem properly.