1

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.

Devolus
  • 21,661
  • 13
  • 66
  • 113
  • 1
    open sometimes takes parameter, sometimes not. Why is this? Suggestion: take parameter as `const char *` or `const std::string&` and store in your class as `std::string` – Neil Kirk Jul 17 '14 at 13:25
  • `open()` wihtout parameters ais a different function. So why can the writer not call it? That is the question. – Devolus Jul 17 '14 at 13:29
  • 1
    If you follow the IContainerWriter path, it doesn't have an implementation of open() or close(). – cup Jul 17 '14 at 13:33
  • Is open() to return if the writer is currently open? is_open would be more intuitive. – Neil Kirk Jul 17 '14 at 13:34
  • 1
    @Devolus `open()` is declared in `IContainer` but called from a `IContainerWriter*`. The `open(char * s)` declaration hides the inhertied `open()` member function. – François Moisan Jul 17 '14 at 13:35
  • 1
    @cup That's irrelevant to the problem at hand. – jrok Jul 17 '14 at 13:38
  • @FrançoisMoisan, I read the link to the duplicate, so apparently this is indeed the case. The first comment that ac tually hits the nail. ;) – Devolus Jul 17 '14 at 13:38
  • IMO this seems a bad design decision on C++. So my solution is now, that I renamed the function in the Writer part to something appropriate to avoid the name hiding. – Devolus Jul 17 '14 at 13:47

0 Answers0