Why can't I access the protected member of a class when deriving it with CRTP pattern?

42 Views Asked by At

Why can't I access parent's class from derived class when using CRTP patten (see Base class)?

template <typename Derived>
class Common {
protected:
int common;
};

template <typename Derived> 
class Base : public Common<Derived> {
void print() {
    // Why is common not accessible from Base? 
    std::cout << common << " " << base << "\n";
}
protected: 
int base;
};

class Main : Base<Main> {
void print() {
    // can access common
    std::cout << common << " " << base << "\n";
}
};

Godbolt: https://godbolt.org/z/99TfjYGKd

0

There are 0 best solutions below