I'm implementing a static polymorphism:
template<typename T>
class Base {
public:
    void method() {
         // do something
         impl();
         // do something else
    }
    void impl() { // intended to be private
        static_cast<T*>(this)->impl();
    }
};
class Derived : public Base<Derived> {
public:
     void impl() { // intended to be private
     }
};
This code is a static implementation of a dynamic polymorphic classes where void impl() was virtual and private.
I've achieved polymorphism (static). But I had to make the method void impl() public in order to allow access to it from a Base class. I want method void impl() to be private again. Can it be done?
UPDATE: I don't want to write
friend class Base<Derived>;
in Derived class, because it allows Base access to all members of Derived.
Adding a line to your Derived class:
class Derived : public Base<Derived> {
public:
private:
     void impl() { // intended to be private
     }
     friend class Base<Derived>;
};
which will specify the base class as a friend, allows me to compile the code with impl being private.
EDIT
As suggested in the comment by @Oliv, the implementation of the Attorney-Client idiom would look like:
template<typename T>
class Attorney;
template<typename T>
class Base {
public:
    void method() {
        // do something
        impl();
        // do something else
    }
private:
    void impl() { // intended to be private
        Attorney<T>::impl(*static_cast<T*>(this));
    }
};
class Derived : public Base<Derived> {
private:
    void impl() { // intended to be private
    }
    friend class Attorney<Derived>;
};
template<typename T>
class Attorney {
private:
    friend class Base<Derived>;
    static void impl(Derived & c) { // add additional parameters as needed by impl
        c.impl();
    }
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With