Advertisement

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

Started by July 15, 2012 10:27 PM
0 comments, last by TechRogue 12 years, 4 months ago
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?
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.

This topic is closed to new replies.

Advertisement