Pointer question (I believe)

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.

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.

You haven't defined/described the DoSomething() method, so your statement can not be verified.

SuperLib is creating two instances of MyLib, but there is no indication that either is ever used.

I am pretty sure it is because they are both using the same chunk of memory

What does "they" refer to? aSuperLib and bSuperLib instances are NOT both using the same chunk of memory.

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?

It isn't a pointer issue, since the snippets you posted don't involve pointers. The compiler allocates the memory when the constructor is invoked.

You'll need to post your real code, I think. Or, at a minimum a complete/compilable example that illustrates the problem, with output.

Sorry for the LONG post, just wanted to make sure I explain the problem properly.

I'm afraid you didn't, though.

Paul, thanks for the reply.

I probably stated it incorrectly, I was thinking my problem was that I need to use a pointer somewhere, and that I was not using one.

DoSomething is pretty simple, without posting a ton of code to really explain what I am doing here, lets just say DoSomething does this:

unsigned short int SuperLib::DoSomething()
{
return aLib.ID + bLib.ID;
}

what I would expect in my sketch is:

1+2 for aSuperLib

and 3+4 for bSuperLib

so, 3 and 7

as the results of my println's

Instead, I am getting the exact same results for both, that was why I thought I needed to use a pointer, I assumed that they were using the same chunk of memory, the results would be the 7, not the 3, which leads me to believe the second one is overwriting the first one.

To be sure, I did one at a time, and I got 3, and 7 respectfully

I probably stated it incorrectly, I was thinking my problem was that I need to use a pointer somewhere, and that I was not using one.

No. You don't need to use pointers. Some situations might make sense. I'm not sure that this one does.

DoSomething is pretty simple, without posting a ton of code to really explain what I am doing here, lets just say DoSomething does this:

How about if we don't. Wading through a ton of code is better than guessing what the problem might be. For both of us,

I guess from the code that you expect aLib and bLib to be instance variables in SuperLib, but they are actually declared as global variables.

SuperLib(unsigned short int aID, unsigned short int bID)
{
aLib=MyLib(aID);
bLib=MyLib(bID);
}

It's hard to be sure, though, because you're mentioning constructors but the code you posted shows plain old functions and no classes. Obviously, since you aren't using classes you aren't using instance variables. Perhaps if you'd posted real code instead of pseudocode, this would have been more obvious. When you post code to show a problem it needs to be real code.

but the code you posted shows plain old functions and no classes.

Well I feel like an idiot for missing that.

When you post code to show a problem it needs to be real code.

Agreed. And, don't worry about how much. Real code that illustrates a problem is better than an attempt to reproduce the problem on a smaller scale, if it doesn't really reproduce the problem.