Jump to content

  • Log In with Google      Sign In   
  • Create Account


* * * * * 1 votes

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 thatdudeoverthere   Members   -  Reputation: 136

Like
0Likes
Like

Posted 15 July 2012 - 04:27 PM

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?

Sponsor:

#2 Jake Albano   Members   -  Reputation: 609

Like
1Likes
Like

Posted 15 July 2012 - 06:58 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS