Is it possible, to somehow allow parent to access child protected members?
template <class T>
class B {
public :
void print()
{
cout << T::a << T::b << endl;
}
};
class C : public B<C>
{
protected :
static int a;
static int b;
public :
C() {
print();
}
};
This will be useful for me to inherit multiple objects without polymorphism(virtual). Any suggestgions??
Edit:
I find two solutions as suggested below ::
- make the B as a friend class,
- CRTP
Few more points to consider, while using CRTP make sure you use inline other wise it won't make it any faster(but code bloat may happen). Do not forget to make the B constructor protected(in case of static derived data access).
CRTP can also be used to not transfer static constant data(virtual static const) from base class to derived
The modern compilers use a concept called devirtualization i think it is in most compilers now.
friend
of B.friend class C
inB
for each subclass, but you only need one linefriend class B
in each subclass.