I am trying to use a CScriptArray to store instances of a derived class and am running into problems when using a dynamic cast to get a pointer to the derived class object.
Quick summary of structure:
HW_State is a class that has a CScriptArray that stores objects of type Component.
Component is a base class to Tranceiver, which derives Component.
An object of type Tranceiver is stored in the CScriptArray
Relevant Code:
Structure of HW_State
class HW_State {
CScriptArray* components; //array of components
}
Instantiation of HW_State:
asITypeInfo* t_component = engine->GetTypeInfoByDecl("array<Component>");
// Create an array with the initial size of 1 elements
components = CScriptArray::Create(t_component, 1); //array of components
components->SetValue(0, new Tranceiver());
Code that generates error (segfault because dynamic cast returns null pointer):
HW_State hwstate01;
//other code to initialize hwstate01
Tranceiver *tx;
Component *com;
com = reinterpret_cast<Component*>(hwstate01.components->At(0));
tx = dynamic_cast<Tranceiver*>(com);
tx->on_off++;
I'm not sure whether my problem lies with the implementation of the base/derived classes, my usage of the CScriptArray, or my usage of casting.
Anyone see what I am missing here?