Q:

Restricting access to derived class for a pure virtual function in c++

class A
{    
public:
    void virtual magic() = 0;
    void bar()
    {
        magic();    // this should be legal
    }        
};

class B: public A
{    
public:
    void magic()
    {
        cout<<"implement magic here"<<endl;
    }
};

class C: public B
{
     void foo()
     {
         magic();     // this should not be allowed, i.e. create compile-time error
     }
};

Thus the pure virtual base class A of B shall have access to magic(), but not any derived class C of B. Can this be achieved using access specifiers and/or friend declarations or in any other way?

A:

Basically, you cannot reduce the visibility of a virtual method. Once it's public in A, there's no tidy way to make it protected or private in any of the derived classes.

A:

If you have access to class A, change the access of magic to private:
private:
         void virtual magic() = 0;

Then make class B a friend of class A:
class A
{
    friend class B;

