I have an abstract class as an interface and from this is another interface derived as an abstract class. From this I derived a baseclass and a final class. What I don't understand is whyx I get a compiler error that a function from the base class is not accessible. I have asked a similar question some time agao, but then it was only about the derivation iteself. Because of this I derived the interface classes as virtual, but it doesn't really help.
I have created a simple sample to illustrate my problem.
#include <iostream>
#include <string>
class IContainer
{
public:
IContainer() {};
virtual ~IContainer() {};
virtual void loadDefaults() = 0;
virtual void storeDefaults() = 0;
virtual bool open() = 0;
virtual bool close() = 0;
};
class IContainerWriter : virtual public IContainer
{
public:
IContainerWriter() {};
virtual ~IContainerWriter() {};
virtual bool open(char *s) = 0;
virtual bool write() = 0;
};
class ContainerBase : public virtual IContainer
{
public:
ContainerBase() {}
virtual ~ContainerBase() {}
void loadDefaults() override {}
void storeDefaults() override {}
};
class WriterBase : public virtual ContainerBase
{
public:
WriterBase() {}
virtual ~WriterBase() {}
void setFilename() {}
bool open() override { return true; }
bool close() override { return true; }
};
class Writer : public virtual WriterBase, public virtual IContainerWriter
{
public:
Writer() {}
virtual ~Writer() {}
bool open(char *s) override
{
if(open() == false)
return false;
if(s)
str = s;
else
return false;
return true;
}
bool write() override { return true; }
private:
char *str;
};
int main(int argc, char *argv[])
{
Writer w;
IContainerWriter *iw = &w;
iw->open();
iw->write();
iw->close();
return 0;
}
Erorr:
error: no matching function for call to 'Writer::open()'|
Of course I can add WriterBase::open()
to the call, and then one error goes away, but I still wouldn't be able to call the open()
function with the interface pointer, as I expcted to.
What I really don't understand is, when I remove the declaration of bool open(char *s)
then I can suddenly call the baseclass open()
without any errors.