Linked List Templates and Object Types

This is a discussion on Linked List Templates and Object Types within the C++ Programming forums, part of the General Programming Boards category; Hi, this is quite a long problem which im sure can be solved quite easily.
I have a linked list ...

Linked List Templates and Object Types

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

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?

Hi Codeplug, thanks for the reply, ive been away for the past week and unable to get a connection to the internet.

Yes thats the heirarchy im using.

Yea i could use that method, but the only problem i get is the LatticeNode objects are only of the derived types, eg they cannot be of Cell type only its derived classes.

Basically the only hacky way i can think of to get around this is pass a string in and compare it for MORPH or JUXT say and then create them this way, only thing being i would have a problem when this prev and next pointers in the list.

For example

we have a linear list of LatticeNodes all of type Juxtacrine, and i wish to change item 10 in the list to a LatticeNode object with type Morphogen.

The current pointer would be of the new type (Morphogen), but the prev and next would point to incorrect types (Juxtacrine), thus falls my a question : Can the prev and next point to the LatticeNode object

If i hard code the n's type to be Morphogen, and use a Juxtacrine T type, the above code requires a copy constructor to compile, but im assuming this doesnt meet my requirements, as this would require all the linked list to change its LatticeNode object type?

I would need a copy constructor to do the assignments, but would the pointers be ok pointing ? If you get what i mean.

Cell is the base class from which Foo and Bar are derived. The display function is virtual in the Cell class and overwritten in both the Foo and Bar classes to provide results specific for each class. In main an instance of each class is decalred and a list of base class pointers is declared. The address of each instance added to the list and then polymorphism is used to display the information in each of the items in the list based on what class each item is. Then the second item in the class is identified, erased, and replaced with an instance of the other derived class. Finally, the information in the modified list is redisplayed.

You need to always hold Cell* in the LatticeNode (I'm not sure it is even necessary to make it a template). Then always assume you have a Cell* in each node. Any behavior that is different between a Juxtacrine and a Morphogen should be implemented in a virtual function that can be called from a Cell*. Then, the appropriate derived class version of that virtual function will be what is actually executed.

The only time you would need to know whether it is a Juxtacrine or a Morphogen is when you create a new object. In that case, you just create the new object and pass it into the replace function as a Cell* (or as a T if you keep the templated version). Here's a quick example of that concept inside your code.

Hi ive been trying to implement to a simple virtual function to test out virtual functions, but im running into an exception when calling it. its called print() and is implemented the same as in elads reply. Im using the below code to call it:

Code:

cout << current->getCell().print << endl;

Where getCell is defined as:

Code:

Cell *t;
Cell& getCell() { return *t; }

Now when it calls the print() function its should print "im a cell" etc. but instead prints a value 1.

If i call this function like so:

Code:

current->getCell.print();

Then i get an exception. Do i have to call it as in elads each time? or am i doing something wrong?

Oh and by the way ive re-implemented the LatticeNode class to not use a Template, now, and hopefully when this print() thing works, i can test out the replace function.