0

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! :)

  • You can't call a member function of the derived class on an instance of the base class. – interjay Apr 13 '15 at 12:57
  • 4
    "And I have this overridden version". This isn't an **overridden** version. You're overloading the virtual function with a non-virtual overload. If you want to override something, the parameter list must be the same, in your case, empty. – Armen Tsirunyan Apr 13 '15 at 12:58
  • 2
    Two problems, declaring `Personality Pclass;` causes `Pclass` to be a `Personality` object, no information exists about any child classes (also read about [object slicing](http://en.wikipedia.org/wiki/Object_slicing)). The second problem is that `Personality::Start` and `example::Start` are not the same functions, because one doesn't have an argument and the other do, and the `example::Start` will *hide* the `Personality::Start` function. – Some programmer dude Apr 13 '15 at 12:59
  • Well, if I can't do that, I will need a method of sending an instance of the FuzzyObject the Pclass is attached to to the overloaded function. I need this so that the derived classes can modify the objects they're attached to. – FuzzyQuills Apr 13 '15 at 13:24
  • after update: I hope you're not adding elements using myArray[i] - you should to use `push_back()`. Your original reference as well as the pointer solution should both work fine. wrote a small example for you [here](http://coliru.stacked-crooked.com/a/05500afbb78de1ad). – ArnonZ Apr 14 '15 at 08:00
  • My goodness I'm stupid... Check up top for changes... :D – FuzzyQuills Apr 14 '15 at 08:30

1 Answers1

3

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.

Community
  • 1
  • 1
ArnonZ
  • 3,822
  • 4
  • 32
  • 42
  • Thank you for your help in this. I actually use your first example currently. I also had another question to ask. (Check up top, I added it to my question) – FuzzyQuills Apr 14 '15 at 07:29