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

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -

PostgreSQL 9.x - pg_read_binary_file & inserting files into bytea -