0

So my problem with this is that I don't know of a way to access fields of a subclass by only having a pointer to the base class. I was gonna ask how to do that instead, but I thought there could be a better solution for the Linked list.

Antonio Craveiro
  • 121
  • 1
  • 11

2 Answers2

1

So if you have a parent class like Parent and a linked list of Parents, you could use dynamic_cast to cast it to the child class as follow:

Child* child = dynamic_cast<Child*>(linkedlist_Parent_pointer);

You could find more info about dynamic_cast here: MSDN

Linked list pseudo-code example: Imagine you have a parent class, let's say: P, then you have A, B and C, they are inherited from P, so you could have a linked list of P:

A* a;
B* b;
C* c;
P* p;

Node<P*>* linked_list(a);
Node<P*>* node2(b);
Node<P*>* node3(c);
Node<P*>* node4(p);

linked_list.InsertAfter(node2);
node2.InsertAfter(node3);
node3.InsertAfter(node4);

/*So, this way you have a linked list containing elements of type A, B, C, P, which are all of type P respectively.
If you want to get a value of a node, let's say, the value of the first node of the linked list:
*/

A* a_val = dynamic_cast<A*>(linked_list.val);

All this taking in mind a node class like:

template<class T>
class Node
{
public:
Node(T* value)
{val = value;}

InsertAfter(Node<T>* node)
{next = node;}

T* val;
Node<T*>* next;
}

This is just a reduced example to get the idea, hope this could help you.

yosbel
  • 1,711
  • 4
  • 20
  • 32
  • I don't see how I could do it with just dynamic cast, I was thinking I would need to at least make 1 type of node for each type of subclass. And the pointer to next node, couldn't I initiate it as void and depending on the subclass use static casts? – Antonio Craveiro May 16 '14 at 17:42
  • You could use the base class as the type your node contains, then when you want to obtain the value extract the "object" class from your node, as it is a base class and if you know the child class type you could use dynamic_cast to get the value, if you want more info about static_cast and dynamic_cast check here: http://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used – yosbel May 16 '14 at 20:22
  • Do you have an example with link lists that use different child classes? Because from what I read it seems this is impossible to implement without a workaround which makes me think it would simpler to integrate all the child and base class in just one big class. – Antonio Craveiro May 19 '14 at 22:15
0

Typically, the base class has enum type, then inherited class has the type filled in.

class base
{
  base() { t = TYPE_UNKNONW; }; 
  base (enum type) { t = TYPE_A; };
  enum  type { TYPE_UNKNONW, TYPE_A, TYPE_B };

  protected:
     enum type t;
}

class A : public base
{ 
    A (enum type x) : base (x) {};
}

Then in the memeber function, you can have a switch statement. This is a alternative when dynamic_cast is not available.

m. c.
  • 867
  • 5
  • 12