Search code examples
c++templatesprotected

Template class hierarchy friend inheritance


I'm tinkering with C++ and are facing the following problem:

Given:

class A {
    int aVar;

public:
    A(){}
    virtual ~A(){};
};

template<class BASE>
class B: public BASE {
    int bVar;

public:
    B() : BASE() {}
    virtual ~B() {}
};

template<class BASE>
class C: public BASE{
    int cVar;

public:
    C() : BASE() {}
    virtual ~C() {}
}

D,E,F,G...

I can do a sort of branched class hierarchy that inherits parameters and functions and such. Like typedef C<B<A>> ABC;

However, I am supposed to have class as well that "manages" certain combinations of these objects through protected access.

template<class TYPE>
struct Manager {
    TYPE current;

    Manager() {
        current.aVar++;
        current.bVar++;
        current.cVar++;    
    }
    ~Manager(){}
};

And my question is: How do I set up the A, B, and C classes to friend Manager regardless of Manager's template type? (e.g. Manager with template ABC, EABC, DABC, XYZEFGABC, ...)


Solution

  • Just have them friend any manager:

    class A {
        template <typename T>
        friend struct Manager;
    };
    
    template<class BASE>
    class B: public BASE {
        template <typename T>
        friend struct Manager;
    };
    
    // etc.