Advertisement

Dynamic_Cast fail with CScriptArray and Derived Class

Started by May 07, 2017 11:34 PM
1 comment, last by calzone 7 years, 6 months ago

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?

Looks like you're missing a @ in

asITypeInfo* t_component = engine->GetTypeInfoByDecl("array<Component>");

With this you're saying that the array should hold values of type 'Component', but you probably meant it to hold references to instances of type 'Component', i.e. "array<Component@>". The different is that when holding values, the addition of new elements will take a copy of the instance, and not refer to the original object.

The difference is similar to std::vector<Component> and std::vector<Component*> in C++.

PS. Don't forget to change how you interpret the array elements after including the @, as they will now be pointers and not object instances.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement

That was it. Thank you.

For those curious, the following Stack Overflow threads shed light on what was happening here:

Pointer to array of base class, populate with derived class

C++ array of base class which has instances of derived classes stored in the elements of the array

Corrected 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
Tranceiver *temp = new Tranceiver();
components->SetValue(0, &temp);

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++; 

This topic is closed to new replies.

Advertisement