Jump to content
  • Advertisement
Sign in to follow this  
thatdudeoverthere

Must inherited classes in C++ be registered as reference types?

This topic is 2165 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'm curious to whether this is true, that classes that are a 'base' class or a 'derived' class must be registered as a reference type and cannot be registered as a value type. I am planning on expanding this example with more complex classes for my applications, but wanted to cover this before I moved on and have to rewrite code accordingly.

For example: (C++)

[source lang="cpp"]
class Base
{
public:
virtual void execute() = 0;
};

class Derived : public Base
{
public:
Derived();
~Derived();

virtual void execute();

int value;
};

[/source]

And this is how I registered them for angelscript NOTE: addRef() & Release() are left out for clarity.
(asOBJ_REF type)
[source lang="cpp"] // Register the derived type
result = asEngine->RegisterObjectType("derived", 0, asOBJ_REF ); assert( result >= 0 );
result = asEngine->RegisterObjectBehaviour("derived", asBEHAVE_ADDREF, "void addRef()", asMETHOD(Derived, addRef), asCALL_THISCALL); assert( result >= 0 );
result = asEngine->RegisterObjectBehaviour("derived", asBEHAVE_RELEASE, "void Release()", asMETHOD(Derived, Release), asCALL_THISCALL); assert( result >= 0 );
result = asEngine->RegisterObjectProperty("derived", "int value", asOFFSET(Derived, value)); assert( result >= 0 );
result = asEngine->RegisterObjectMethod("derived", "void execute()", asMETHOD(Derived, execute), asCALL_THISCALL); assert( result >= 0 );[/source]
And this is how I registered them for angelscript
(asOBJ_VALUE type)
[source lang="cpp"] result = asEngine->RegisterObjectType("derived", sizeof(Derived), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CD); assert( result >= 0 );
result = asEngine->RegisterObjectProperty("derived", "int value", asOFFSET(Derived, value)); assert( result >= 0 );
result = asEngine->RegisterObjectMethod("derived", "void execute()", asMETHOD(Derived, execute), asCALL_THISCALL); assert( result >= 0 );[/source]

When I use the asOBJ_REF version of the code it works properly, but when I use the asOBJ_VALUE version I receive a access violation. So my question is whether this is true or am I registering the value type version incorrectly?

Share this post


Link to post
Share on other sites
Advertisement
It says here that hierarchies are only supported for reference types. I think this is because a cast must be able to return a null handle if the cast fails, which can't be done with value types.

That much aside, why are you registering the value type with asOBJ_POD? A POD data structure can only hold other POD types (int, float, char and pointer), and can't have a destructor. That might what your problem actually is, since it looks as though you aren't trying to cast between base and derived types anyway.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!