Thanks for advice guys, Ill try and make it more clear what I am trying to accomplish.
In visualization terms think of a box, this box can hold exactly 9 objects in it,each of the 9 spots is a numbered index 0-8, but only objects of either type A or type B. I would like a associative container that can keep track of which everything in the box, including which type is located at which box index. I tried doing this through inheritance and downcasting but I feel like there should be a better way. I have data members in A and B that extend a baseclass. The classes look similar to this:
Class BaseClass
{
public:
BaseClass(string, int, string, int, int, int);
~BaseClass();
void SetName(string value) {name = value;}
void SetType(int value) {type = value;}
void SetFlavor(string value) {flavor = value;}
void SetNum1(int value) {num1 = value;}
void SetNum2(int value) {num2 = value;}
void SetNum3(int value) {num3 = value;}
const string GetName() {return name;}
const int GetType() {return type;}
const string GetFlavor() {return flavor;}
Const int GetNum1() {return num1;}
const int GetNum2() {return num2;}
const int GetNum3() {return num3;}
protected:
string name; //name of object
int type; //flagged 0 for type a and 1 for type B
string flavor; //flavor text about object
int num1;
int num2;
int num3;
}
Class A : public BaseClass
{
public:
A(string name, int type, string flavor, int num1, int num2, int num3, int num4
int num5, float speed)
:BaseClass(name, type, flavor, num1, num2, num3);
~A();
//getters and setters for speed, num4 and num5...
private:
float speed;
int num4;
int num5;
}
Class B : public BaseClass
{
public:
B(string name, int type, string flavor, int num1, int num2, int num3 int bInt)
:BaseClass(name, type, flavor, num1, num2, num3);
~B();
void SetBInt(int value) {bInt = value;}
const int GetBInt() {return bInt;}
private:
int bInt;
}
so that when in class box it would look like this:
//Box.h
#include "A.h"
#include "B.h"
#include "BaseClass.h"
Class Box
{
public:
Box();
~Box();
void AddObjectTypeA(A* objectA);
void AddObjectTypeB(B* objectB);
private:
map<int, BaseClass*> myBoxMap;
}
//Box.cpp
void Box::AddObjectTypeA(A* objectA)
{
bool full = false;
for(int i = 0; i < myBoxMap.size(); i++)
{
if(i == myBoxMap.size())
{
//map is full - only want 9 objects inside no more
full = true;
break;
}
if(mFoundOffensiveMap->GetOffCellName() != "") //preloaded during box construction with 9 objects inside of type Basecell name = ""
{
//bad choice cell full already
continue;
}
else if(mFoundOffensiveMap->GetOffCellName() == "")
{
//good cell choice - it's empty currently
myBoxMap->SetName(cell->GetName());
myBoxMap->SetType(cell->GetType());
myBoxMap->Setnum1(cell->Getnum1());
myBoxMap->Setnum2(cell->Getnum2());
myBoxMap->Setnum3(cell->Getnum3());
myBoxMap->Setnum4(cell->GetNum4());
myBoxMap->setSpeed(cell->GetSpeed());
myBoxMap->setnum5(cell->Getnum5());
break;
}
}
return full;
}
finally in main.cpp
//main.cpp
int main()
{
A* myObjA = new A("Steve", 0, "is a man", 4, 6, 7, 17, 8, 3.4f);
Box* myBox;
myBox->AddObjectA(myObjA);
/* during the above call I would like to set myObjA into the Box
at the first available index without another object. It tests
this in the function by checking if name = "". */
}
I hope that better explains it. As it currently stand I'm getting access violation errors when in the function to add. Those errors are keeping me from knowing if this set up will actually work for what I want. Thank you again, for reading into this. I truly appreciate the quick responses you have offered.
Regards,
Soundstone