c++ - Why crtp works for structs and not for class? -
let's consider following code:
template<typename t> struct base{ }; struct derived: base<derived>{ }; int main() { base<derived>* base_ptr = new derived(); }
and works, mean compiled. , same version of class:
template<typename t> class base{ }; class derived: base<derived>{ }; int main() { base<derived>* base_ptr = new derived(); //error ‘base<derived>’ inaccessible base of ‘derived’ }
because struct default access base classes public
, , class it's private
.
use
class derived: public base<derived>{ ^^^^^^
to make equivalent first example.
this has nothing crtp, same error without crtp:
class base { }; class derived : base { }; base* base_ptr = new derived();
Comments
Post a Comment