Reputation: 1
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.
Upvotes: 0
Views: 328
Reputation: 6039
C++ does not have interfaces. You create an abstract class with a pure virtual method.
class MonoBehaviour
{
virtual void Update() = 0;
};
class your_class : MonoBehaviour
{
void Update()
{
// implement here.
}
};
Upvotes: 5
Reputation: 758
First things first. The example you provided will not work firstly because you forgot the instantiation of your pointer. You probably meant smth like this:
Base* base = new Object;
while(1)
{
base->Update();
}
Now with this out of the way. This concept should work as you expect. Update should be called in the Object class if Update overrides a virtual function in base as already pointed out by others. There is a neat thing in c++11
called override
. It is a keyword that you put after the declaration of the function that you expect to override some virtual function in the base. It is a good practice to use override
as the compiler will complaint if you try to override
a non-virtual function. This reduces the possibilities for errors in the code.
Upvotes: 0
Reputation: 51
There might be the signatures of the child class method and the base class method are not the same, i.e. they have the same name but return different things or have different parameters. That's one way to always call the base class method.
Or there might be you are simply constructing base class objects and not children class objects in the main function. Put a break in the main function to inspect the actual type of the object(s) you are calling Update for.
The code you've posted should work as desired. It doesn't matter if the base class is abstract or not. I prefer to make it concrete and throw exceptions in the methods that should be =0 for the sake of easiness when implementing the children classes (I don't have to implement the full behavior from the start, just the methods I am focused on and which I am testing; so it goes incrementally).
And yes, stick with public inheritance.
EDIT: Or there might be object slicing involved if you have something like this:
Derived* d_ptr = new Derived();
Base b = *d_ptr; // object slicing, d's methods will become Base methods;
Base* b_ptr = &b;
b_ptr->Update(); // will call Base::Update()
Upvotes: 0