tags:

views:

108

answers:

2

I have a base 'node' class with an 'update()' member function

now i have other classes that extend the 'node' class and override the 'update' class.

but all my 'node's are stored in an array (of type 'node') and they all get 'update'd in turn.

The problem is the its calling the base node class update rather than my override function.

What am i doing wrong? (i have the @Override and the functions are the same name, return type and have the same params)

EDIT OMG SORRY I WAS EDITING THE BACKUP FILE. WORKS NOW. SORRY SORRY

A: 

Try checking the node subtype and casting before calling that function.

steadfastbuck
This is horribly wrong.
ChssPly76
I have todo this for every sub class? because that makes extending a pita.
tm1rbrt
As ChssPly76 said this is horribly wrong!! This solution goes exactly on the opposite way of what is searching tm1rbrt: genericity in its treatment.
reef
A: 

You should declare your array of type derived

What i understand you have is :

Node[] array = {new node1, new node2}

You should have:

Node[] array = {new derived, derived}

The key of the issue, is to instantiate objects of type derived, but treat them as Nodes.

for example

void foo (Node node){
  node.update()
}

bar(){
   foo (new Node())
   foo (new Derived())
}
Tom