My goal is to have a base class that deals with all the stuff that the Derived ones will have in common, but then allows each of the derived classes to implement their specific stuff on their own, without the Base knowing or caring about any of it, and still somehow be used in a polymorphic pointer. The derived classes are supposed to have distinct usefulness, so they'll have different functions altogether. One might deal with sprites, the other with text, etc, but, again, they do share some background/backstage functionalities.
Currently I'm declaring a pointer of type Base*, and defining it as type Derived*. And it works fine but only until I call one of Derived's specific functions. I get this error:
'class Base' has no member named 'function'.
I understand why that error occurs: I'm accessing a Derived through a Base pointer, and sure enough, Base knows nothing about that function because it was not declared in Base. That might be one solution, to declare it in Base, but that would then force me to also declare every other function specific to each derived class, in the Base class, and that's what I'd like to avoid.
Is there a way to achieve this?
Some (pseudo)code, illustrating what I'm doing and what I'd like to achieve (streamlined for simplicity, assume everything is public):
base.h
class Base
{
virtual ~Base() =0;
virtual void funk() =0;
void common() {} // deal with shared stuff
// knows nothing about der1::draw() and der2::write()
};
der1.h
class Der1 : public Base
{
void funk() { ... }
void draw(); // specific to this class
};
der2.h
class Der2 : public Base
{
void funk() { ... }
void write(); // specific to this class
};
elsewhere.h
Base* ptr1;
Base* ptr2;
elsewhere.cpp - this is my actual goal
ptr1 = new Der1();
ptr2 = new Der2();
ptr1->draw();
ptr2->write();