I want to implement a behavior I saw in an engine (Unity3D) in a C++ project.
The following code is in C# (Unity’s language):
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Update() {
//Update is called every frame
}
}
Since I don’t have access to MonoBehavior source I can only guess that Update() is a virtual function that can be implemented like this.
I want to replicate the same behavior in C++. Have a loop in main() and a super object will have it’s Update() method called in that loop and all it’s children should inherit this super object and have the possibility to implement Update() and use it.
I know it’s an ambiguous question but I’ve searched everywhere for an answer and didn’t found one.
Here is an example:
class Base
{
public:
virtual void Update();
};
class Object: public Base
{
public:
void Update();
};
void main()
{
Base* base;
while(1)
{
base->Update();
}
}
And the result should be that Object’s Update() should be called through Base. I am 100% the code above doesn’t work and that is why I am in need of some ideas.
C++ does not have interfaces. You create an abstract class with a pure virtual method.