c++ - Using a class that inherits a template class as a constructor parameter in the template class? -
say have following:
template <class t> class foo {};
and descendant class,
class bar : public foo<some_typename>{};
how go passing bar foo's constructor without foo.h including bar.h, if possible?
it's unusual, there ways around it, depending on needs (elaborate, perhaps?).
understand makes sense pass bar foo.
in case, could:
1) create second template parameter in foo , lay out interface required @ construction:
template < typename t, typename bar_ > class foo { /* ... */ foo(bar_& bar) : weight_(bar.getweight()) { } /* ... */
2) or use template ctor:
template < typename bar_ > foo(bar_& bar) : weight_(bar.getweight()) { }
3) if foo+bar lightweight, create extended initialization list, used bar @ init.
4) since template (and implementation must visible subclass , has special linkage), declare foo ctor takes bar pointer, define foo constructor in bar.hpp
Comments
Post a Comment