Jump to content

Virtual functions calls don't always go to the child-most class


photo

Recommended Posts

Posted

In an example I have 3 classes set up like this 

class Parent
{
	virtual void MainFunction()
	{
		log.message( "Parents MainFnction\n" );
	}

	virtual void NextFunction()
	{
		log.message( "Parents NextFnction\n" );
	}
};

class Child : Parent
{
	virtual void MainFunction()
	{
		log.message( "Child MainFunction\n" );

		NextFunction();
	}

};

class GrandChild : Child
{
	virtual void NextFunction()
	{
		log.message( "GrandChild NextFnction\n" );
	}
};

If I create a GrandChild object and call MainFunction

GrandChild gc = new GrandChild();
gc.NextFunction();

I would Expect the Child's version of MainFunction to get called which in turn should call the GrandChilds Version of NextFunction, but instead in Unigine Script The output is...

 

Child MainFunction

Parent NextFunction

 

For some reason the child-most class isn't getting called.  If I do the same thing in C++ I get what I expect to happen the Child MainFunction calls the NextFunction in the GrandChild Class.

Posted

Hi Dusty,

 

That is known issue. Unfortunately, the inheritance concept in unigine script is not perfect and you have to override MainFunction in GrandChild as well.

Posted

So they only safe way is to have empty versions of every virtual function in every derived class? That seams extremely fragile. 

×
×
  • Create New...