Reputation: 25
Now, this question does seem to have been asked millions of times before, but I think mine's a special case...
In any case, I have this class:
class Personality {
public:
Personality() {};
virtual void Start () {
}
virtual void Update () {
}
virtual ~Personality() {};
};
And I have this overridden version:
class example : public Personality {
public:
example() {};
void Start (FuzzyGame::FuzzyObject& o);
void Update (FuzzyGame::FuzzyObject& o);
~example() {};
};
Then I have my GameObject class (aka. FuzzyObject):
class FuzzyObject {
public:
std::string name;
std::string tag;
std::string type;
FuzzyForm xform;
bool enabled = false;
std::string className;
Personality Pclass;
std::string path;
std::string texPath;
MD2Object animMesh;
bool isMD2;
LightData light;
CamData cam;
GLuint Texture;
BillboardType BBType;
};
What I would like to know: is it possible to call a method, say Update(), from the example() class, with this instance of the base class, with some way of passing the class it is attached to (the "FuzzyObject") to that function?
Personality Pclass;
Basically, this is all so I can have a C++-based scripting system for my game engine.
UPDATE: From reading the answer I approved, I am actually using this way of doing it currently:
example e;
e.Start(/*whatever should be here*/);
But I found feeding the vector element in directly caused a segfault. I tried a pointer to the vector element, and that worked. sort of... Anyone know why feeding in a vector's element like this:
example.Start(&myArray<FuzzyObject>[i]); //assuming this is in a for-loop
Into my overloaded function modifies ALL the elements in my vector?!
aka. when running Update on one object (the code modifies the transform component to offset the affected object by 0.1) it offsets all the other objects too, which is obviously not what I want. Anyone know the cause of this weird behavior?
All I can assume is that the root of the problem is some weird memory allocation problem that only C++ would cook up... :D
UPDATE 2: I finally found the source of my other problem (update 1), and it was something REAL stupid that would only occur in OpenGL... :D
It turns out the reason it APPEARED to be modifying the whole bang lot in std::vector was actually not the vector pointer at all; it was because I had forgotten to load the identity matrix before drawing the next object! And likewise, as one was being moved, the previous transform made the others decide to drift along like drunken sailors... ;)
And likewise, sorry to waste you guys time... :D I should have known better to reset the matrix first! :)
Upvotes: 0
Views: 713
Reputation: 3860
Absolutely not.
There are several problems with your code:
First of all your Start
and Update
methods don't have the same signature. You'll need them to have the same signature to use any sort of polymorphic behavior. Otherwise it's an overload not an override (and a hidden one at that).
Secondly, To use a derived class's methods, the instance have to actually be of the derived class type.
Meaning, You have to either hold an instance of example
:
example e;
e.Start(/*whatever should be here*/);
Or use a polymorphic Personality
to hold an instance of example
:
Personality *p = new example();
p->Start(); // calls example's Start() in case it has a parameterless Start() method
The ladder can be done with a reference as well:
example e;
Personality &p = e;
p.Start(); // calls example's Start() in case it has a parameterless Start() method
Of course you could just call e.Start()
in the last example but the idea is that those three lines might actually be located in different places of your code.
On a side note, what you're asking the compiler to do is not really possible logically.
If FuzzyCode
depends on the base Personality
and doesn't depend on the derived example
in any way, then it's not possible for a change in example
to affect FuzzyCode
.
Upvotes: 3