Hi, this is quite a long problem which im sure can be solved quite easily.
I have a linked list data structure, which roughly represents a Hexagonal lattice (6 sided figure). For the project im doing each hexagon represents a Cell. Now i have a several different types of Cells (Morphogen & Juxtacrine to be exact - these are derived from the base class Cell)
For my data structure im using a similar construct, using templates, to create an lattice.
Code:
Lattice<Juxtacrine,500> lat;
Where Juxtacrine is the derived object type and 500 is the number of elements in the lattice.
Im working on a function to be able to replace an Object at [i,j] in the lattice with a different object type (eg Morphogen) eg
Code:
Lattice<Juxtacrine,500> lat;
Morphogen *m = new Morphogen();
lat.replace(25,43,m);
Thus replacing 25,43 with a new Morphogen object as apposed to a Juxtacrine object.
This is the LatticeNode object which a Lattice is made of:
Code:
template <class T> class LatticeNode {
public:
T t;
void set(int i, int j);
void setI(int x) { i = x; }
void setJ(int x) { j = x; }
void setNum(int i) { num = i; }
void inc();
void dec();
void inc_i() { i++; }
void inc_j() { j++; }
LatticeNode<T> *next;
LatticeNode<T> *prev;
T* getCell() { return &t; }
//default constructor
LatticeNode();
int getI() { return i; }
int getJ() { return j; }
int getNum() { return num; }
private:
int num;
int i;
int j;
};
This is the function:
Code:
template <class T, int size>void Lattice<T, size>::replace(int i, int j, T *t)
{
remove(i,j);
int r = calc(i,j);
LatticeNode<T> *n = new LatticeNode<T>;
if(r < this->length())
{
current = head;
for(int k = 0; k < size; k++)
{
if(current->getI() == i && current->getJ() == j)
{
n->next = current->next;
n->prev = current->next->prev;
current->next = n;
n->next->prev = n;
n->setNum(current->next->next->getNum());
n->set(i,j);
current = n;
incLength();
for(int l = k; l < length(); l++)
{
if(current->next != NULL)
{
current->next->inc();
current = current->next;
}
}
}
if(current->next != NULL)
current = current->next;
}
}
else
{
//add to end of list
current = tail;
current->next = new LatticeNode<T>;
current->next->prev = current;
incLength();
current->num = length();
}
reset_coords();
}
How do i get the passed parameter to T *t be used instead of the templated T?
Is this a good way to go about this problem? I was thinking of not using a Template on the lattice object, although i would need to re-code a lot... does anyone have any ideas or clues to whether im going about this in the right direction? Do i need to do some form of casting to the object, to make it into the right object?