5

I came across this in one of the posts on SO. I am having difficulty understanding the following code.

class A 
{
public:
    virtual void foo() = 0;
private:
    virtual void bar() = 0;
};

class B : public A 
{
private:
    virtual void foo() 
    {
        std::cout << "B in Foo \n";
    } 
public:
    virtual void bar() 
    {
        std::cout << "bar in Foo \n";
    } 
};

void f(A& a, B& b)
{
    a.foo(); // ok --->Line A
    //b.foo(); // error: B::foo is private
    //a.bar(); // error: A::bar is private
    b.bar(); // ok (B::bar is public, even though A::bar is private) --> Line B
}

int main()
{
    B b;
    f(b, b);
}

Now I wanted to know how Line A is possible ? Normally classA would be an interface and you wouldnt be able to instantiate it. You would only be able to instantiate its implementation. I would appreciate it if someone could explain to me whats happening here.

Update :

is it possible to consider a in LineA as an implementation of b ?

Rajeshwar
  • 11,179
  • 26
  • 86
  • 158
  • You should read *most* of the answers to this question: [C++: overriding public\private inheritance](http://stackoverflow.com/questions/5753833/c-overriding-public-private-inheritance) – WhozCraig Sep 19 '14 at 05:34
  • I'm a little bit unclear here - are you asking why it's possible to accept a pure virtual base class as a parameter? – Shog9 Sep 22 '14 at 03:59

0 Answers0