Jump to content
  • Advertisement
Sign in to follow this  
calzone

Dynamic_Cast fail with CScriptArray and Derived Class

This topic is 581 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

 

 

 

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!