KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I wanted to see if it's possible to create "interfaces", inherit them, and then check at runtime if any random class implements that interface. This is what I have: struct GameObject { int x,y; std::string name; virtual void blah() { }; }; struct Airholder { int oxygen; int nitrogen; }; struct Turf : public GameObject, public Airholder { Turf() : GameObject() { name = "Turf"; } void blah() { }; }; void remove_air(GameObject* o) { Airholder* a = dynamic_cast<Airholder*>(o); if(!a) return; a->oxygen = 0; a->nitrogen = 0; }; Now, it works. The documentation says that it works, the test example works.. But also, it didn't compile until I added a virtual method to GameObject. The thing is, I really don't know if the feature is intended to be used like that. What made me wonder there is the fact that I have to declare a virtual function for the class I'm checking. But obviously, there is none, the class I'm checking itself has no virtual functions, in fact my whole code has nothing to do with virtual functions, it's an entirely different approach. So, I guess my question is: If what I'm doing really works, why do I need a virtual function to give my class a vtable? Why can't I declare the class a "runtime type" or something without virtual functions?
Tags (comma-separated)
Save Edits
Cancel