#include "Object.h"
#include "Drawable.h"
class StaticObject : public Object, public Drawable{
private:
sf::Sprite sprite;
public:
StaticObject();
~StaticObject();
bool update(sf::Time frameTime);
sf::Sprite* getImage();
};
They are also both used in Player.h but Player.h looks pretty much identical to StaticObject with the extra stuff removed so I just didn't post it.
Anyway, down to my problem. When I try to access getImage by casting the current object in the queue (the queue data type is of void*) as a Drawable*, the update function is called instead.
The error I get is saying I am calling a function declared with one calling convention with a function pointer declared with a different calling convention and this makes perfect sense because for some reason, the function pointer is pointed at the virtual function Object::update but I can't figure out why and how to make it point at the virtual function Drawable::getImage.
Also, the virtual update function is called in a different place just before this and works correctly. Can someone that understands a little more about this help me understand what is going on?
You cannot store pointers to polymorphic types in void * because it can cause loss of runtime type information, and you should never use void * to begin with.
Just store Drawable * in your queue to begin with.
I don't understand why you would use a self-made C-style linked list when you're using C++ anyway. C++ should not be used as "C with Classes" - that will get you into big trouble.
I guess I should have clarified, the objects in the queue are also updated via the virtual function. So there is one loop that updates all the objects in the queue and another loop that draws the objects. I am not sure what type to use as the queue type since there are 2 base classes. I will add this to the original post.
You are:
a. implicitly converting a pointer to an object to pointer to void
b. and then converting that pointer to void to a pointer to a different type via a compile-time cast
Instead:
a. implicitly converti a pointer to an object to a pointer to void
b. first restore the type by converting that pointer to void to a pointer to the original type via a compile-time cast
c. convert the result of that cast to a pointer to another class in the same hierarchy
Thanks JLBorges, this helped me fix my probelm. One question though. Why is it that you need to cast the void* back to the original type before casting it as the new base? I changed the queue type to Object* and it still works. So now the original type is Object* and I am able to do a dynamic cast to get at the virtual functions from the Drawable class which is only related because they share a derived class? This is a little confusing. The link that keskiverto posted helps a little. It says that there are multiple vptrs when there is multiple inheretence but how does it figure out what vptr to use when the type changes via a cast? As I typed that last sentence I realised why it wasn't working before but I just don't understand how it works using a dynamic cast now.
> Why is it that you need to cast the void* back to the original type before casting it as the new base?
> I just don't understand how it works using a dynamic cast now.