Original Post
Hey, I'm having two problems while trying to make a game (I'm using Visual Studio 2010 Express)
1) Trouble with renaming a namespace:
What I'm trying to do is put all classes in an namespace of an library I'm using into my own namespace, so I don't have to work with different namespaces.
what i'm doing is:
But If I do this I get all kinds of errors about types that are not convertable when using templates (smart pointers). Like:
2) A bad combination of inheritance, polymorphism and void pointers.
I have a listener class with callback functions which other classes inherit from, using virtual functions the overridden function is called, quite standard. But the catch is:
The physics engine I'm using stores userdata as void* pointers, what I try to do is store the listener in the userdata (as void*) and then cast it to the listener class pointer and call the callback function but that where it goes wrong.
This is the simplified problem:
I'm guessing the error is caused because the casting to void* discards the runtime information about the pointer and then when you cast it back try to use it again. But the question is what to do about it? I'm quite clueless and there don't seem to be any solution without completely redesign my programming.
Thanks in advance.
1) Trouble with renaming a namespace:
What I'm trying to do is put all classes in an namespace of an library I'm using into my own namespace, so I don't have to work with different namespaces.
what i'm doing is:
namespace Engine
{
using namespace sf;
}
// namespace Engine = sf; doesn't work because the namespace already existsBut If I do this I get all kinds of errors about types that are not convertable when using templates (smart pointers). Like:
error C2440: 'initializing' : cannot convert from 'boost::shared_ptr<T>' to 'boost::shared_ptr<T>'
with
[
T=sf::RenderWindow
]
and
[
T=Engine::RenderWindow
]I do understand why this error occurs but is there a work around or do I have to use 2 namespaces?2) A bad combination of inheritance, polymorphism and void pointers.
I have a listener class with callback functions which other classes inherit from, using virtual functions the overridden function is called, quite standard. But the catch is:
The physics engine I'm using stores userdata as void* pointers, what I try to do is store the listener in the userdata (as void*) and then cast it to the listener class pointer and call the callback function but that where it goes wrong.
This is the simplified problem:
class Listener
{
public:
virtual void onEvent() = 0;
};
class MyListener : public Listener
{
public:
virtual void onEvent() {};
};
int main()
{
MyListener* listener = new MyListener();
PhysicsBody.userData = static_cast<void*>(listener);
do things
}
void onEvent(PhysicsBody body)
{
Listener* listener = static_cast<Listener*>(body.userData);
listener->onEvent(); // Runtime error about memory violation;
// dynamic_cast<Listener*>(body.userData) // gives a compilation error
}
I'm guessing the error is caused because the casting to void* discards the runtime information about the pointer and then when you cast it back try to use it again. But the question is what to do about it? I'm quite clueless and there don't seem to be any solution without completely redesign my programming.
Thanks in advance.