🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Virtual Inheritance

Started by
4 comments, last by Nonamee Vektor 5 years, 11 months ago

Hi!

I was reading the documentation how to register classes with hierarchy, however i ran into problems while doing it.

In my cpp project, i have two classes which cannot be changed:



class IEntity
{
public:
virtual int GetType();
};

class IPlayer : public virtual IEntity
{
public:
virtual void DoThing();
};

Registering:


    template <class T>
    void RegisterBaseMembers(asIScriptEngine * engine, const char* type)
    {
        engine->RegisterObjectMethod(type, "int GetType()", asMETHOD(T, GetType), asCALL_THISCALL);
    }
    template <class T>
    void RegisterMembers(asIScriptEngine * engine, const char* type)
    {
        RegisterBaseMembers<T>(engine, type);

        engine->RegisterObjectMethod(type, "void DoThing()", asMETHOD(T, DoThing), asCALL_THISCALL);
    }

//later
RegisterBaseMembers<IEntity>(engine, "Entity");
RegisterMembers<IPlayer>(engine, "Player");

Errors:
 'type cast': cannot convert from 'int (__cdecl IEntity::* )(void) const' to 'void (__cdecl IPlayer::* )(void)'
'asSMethodPtr<16>::Convert': no matching overloaded function found

Seems like it works only if the inheritance is not public virtual, but the problem is, im not allowed to change the IEntity and IPlayer classes.

Thanks!

 

 

Advertisement

Not 100% but i do believe this is because you are trying to cast Iplayer as an Ientity type. So you are asking for the function pointer to IPlayer::GetType() and not IEnity::Gettype(); Because this is an inherited class IPlayer doesnt have this function because its owned by IEnity. So when you say typename T and pass IPlayer and try to get the pointer inside of RegisterBaseMember<T> it is Looking for a IPlayer::* or a member of IPlayer, but get type is a member of IEnity::*. How I would handle this is doing


Struct IPlayer : IEntity
{
   ...
	int GetEntity() { IEntity::Gettype(); }
}

Im probably wrong here Im not 100% sure on how to approach this but this makes sense to me

Im trying to avoid writing wrappers on the original API classes...

Thanks for the answer btw.

Registering methods for classes with virtual inheritance isn't supported. This is because the address for calling these cannot be represented on some compilers and is too tricky on others. You'll need wrappers in order to register these.

Perhaps the autowrapper add-on works here. I haven't tried it though.

 

 

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

Thanks for the answer... In the meantime i wrote wrapper classes to bring it to life.

Anyways, im going to check out that addon. :D

This topic is closed to new replies.

Advertisement