0

I have the following scheme:

class Interface
{
    virtual ~Interface() { }
    virtual void foo() const = 0;
    virtual void bar() const = 0;
}

//Interface is derived privately mostly for preventing upcast outside
class Derived : private Interface
{
public:
    void foo() const;
private:
    void bar() const;
}

It does not compile : foo is private. Is there any way to make it public without adding a dummy public function?

galinette
  • 8,896
  • 2
  • 36
  • 87
  • 1
    There is no way you can call `foo()` from `Derived`, as it is private. You have to make it protected – king_nak Dec 23 '14 at 13:54
  • It compiles fine for me on MSVS 2013. I believe OP is simply overriding `foo` in `Interface` with the `foo` in `Derived`. The reason it didn't compile for me was because the destructor of `Interface` is `private`. – Nard Dec 23 '14 at 14:02
  • This will not even compile, as ~Interface() is private. – arayq2 Dec 23 '14 at 14:03

3 Answers3

2

It is perfectly valid, as far as the language is concerned, for a public member function in a derived class to override a private member function in the base class. Whether doing so is a good idea is a different question. And it certainly makes little sense for an abstract base class to have no public member functions.

The problem with your code is that Interface has a private destructor, making it impossible for derived classes to destroy their base class subobjects. ~Interface() should be either protected or public.

T.C.
  • 133,968
  • 17
  • 288
  • 421
0

No, there is not.

Furthermore, even a dummy public function would require that foo in the base were protected, not private.

I would revisit your design. If the function is intended to be public, then why is it not public?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
-1

It is not allowed to change the accessibility of an inherited member. If it was allowed, you would be able to derive from a class and make its private or protected members public, breaking the encapsulation.

opetroch
  • 3,929
  • 2
  • 22
  • 24