Changing an objects Type

This is a discussion on Changing an objects Type within the C++ Programming forums, part of the General Programming Boards category; Hi im having quite a complex problem with my code and was wondering whether i could get some guidance or ...

  1. #1
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92

    Question Changing an objects Type

    Hi im having quite a complex problem with my code and was wondering whether i could get some guidance or advice. Ill explain the problem im detail.

    If you have read some of my earlier threads you may know that im working on a Cellular model using the following heirarchy:

    Code:
                  Cell
                 /      \
                /        \
            Juxta    Morphogen
    Where Cell is the base object and Juxta and morphogen are derived types.

    Now im using a linked list structure to hold these objects and am using a LatticeNode object to encapsulate this:

    Code:
    class LatticeNode {
    
    	public:
    		Cell t;
    		LatticeNode *next;
    		LatticeNode *prev;
    	
    		Cell* getCell() { return &t; }
    
    		//default constructor
    		LatticeNode(Cell *c);
    };
    In red i have the object Cell. Now ideally i want this to only be of a derived type, and not the cell type. And this is where i run into problems.

    I tried writing an =operator to try to copy the object into a derived type.. but it seems this doesnt work exactly how i want it to.

    Basically when i call the constructor for the LatticeNode object i pass in a pointer to an object type. Eg

    Code:
    T *t = new T();
    
    	head = new LatticeNode(t);
    Where T is a derived type.

    Now when i call this in the constructor;

    Code:
    LatticeNode::LatticeNode(Cell *c)
    { 
    	t = c;
    }
    This makes a copy of the passed in object pointer in t. But however t is still of type Cell, and not the derived type.

    Ive got a =operator in each of the derived classes from the base class, but neither of these are called, it only calls the cell =operator.

    Ive tried changing the definition of Cell t; in latticeNode to a pointer but this completely mucks up my code, and causes crashes when the LatticeNode constructor is called.

    All i need to do is change the base type of the object to the passed in type in the constructor, and it will be fine.

    Is this possible to do or will i need to work it out using a pointer to a cell object in LatticeNode?

    Any help appreciated..!

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    You need to first change your list so that Cell t becomes Cell* t. This is so you don't get object slicing but polymorphic behavior. You also must make sure that Cell has a virtual functions. You should beable to do this with dynamic_cast but you might want to consider puting the functionality in the base class.

    Code:
    if (jux = dynamic_cast<Juxta*>(t))   
    {
             // Juxta derived from Cell
    }
    else if (Morphogen* morpho = dynamic_cast<Morphogen*>(t))
    {
             // Morphogen derived from Cell
    }

  3. #3
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    Hm ok, thanks, ill take a look into this

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21