Reputation: 636
I have a base resource that contains pure virtual functions like load and unload. I have then a class inheritent from this class and be AudioResource and have virtual functions aka like play and stop. I then in inherent from this class and have a class called BassAResource.
Lets imagine I have returned a type of type Resource* then I want to type cast it to AudioResource and call the load functions, and let the class that it actually is handle that but i keep getting problems saying that it is a pure virtual function within AudioResource =s
class Resource
{
public:
Resource();
Resource(std::string filename,unsigned int scope, RESOURCE_TYPE type);
virtual ~Resource();
virtual void load() = 0;
virtual void unload() = 0;
class AudioResource : public Resource
{
public:
AudioResource(std::string filename, unsigned int scope, RESOURCE_TYPE type, AUDIO_T Atype);
virtual void load() = 0;
virtual void unload() = 0;
virtual void play() = 0;
virtual void pause() = 0;
virtual void stop() = 0;
class BASSAResource : public AudioResource
{
public:
~BASSAResource();
virtual void load();
virtual void unload();
virtual void play();
virtual void pause();
virtual void stop();
Upvotes: 3
Views: 2444
Reputation: 11075
You cannot construct an 'AudioResource' as it has undefined methods. I'll assume you're returning a 'BASSAResource' that has been cast to a Resource pointer. Let me pseudo code to see if I get your meaning
Resource *myFunc() {
BASSAResource *resource = new BASSAResource();
return (Resource*)resource;
}
void myMain() {
Resource *resource = myFunc();
AudioResource *audio = (AudioResource*) resource;
audio->load();
}
This should work fine - in fact why cast to AudioResource since load is defined as an abstract method in your Resource class. But if you wanted to 'play', this cast would be required. Again, you cannot construct using 'new AudioResource()' due to the fact that you have an abstract method which must first be implemented.
Also, be very careful to make all destructors virtual. Your ~BASSAResource is not virtual, and this will cause problems for you.
Upvotes: 1
Reputation: 427
Virtual inheritance only works with pointers (you have to reference the object, not the type). Instead, cast it to type AudioResource* and treat it as a pointer (or use a smart pointer).
Upvotes: 1