T
Thomas Matthews
Hi,
I would like to apply inheritance to a template parameter, but my design
fails to compile: cannot initialize one template class with child
child parameterized class. I'll explain...
Given:
#include <string>
// Parent class
class Name_Id
{
public:
Name_Id(const string& new_name,
int new_id = 0)
: name(new_name), id(new_id)
{}
virtual size_t max_width(void) const = 0;
// Other methods here...
private:
string name;
int id;
};
// Child classes
class Category
: public Name_Id
{
public:
Category(const string& new_name)
: Name_Id(new_name)
{ };
size_t max_width(void) const
{ return 32};
};
class Title
: public Name_Id
{
public:
Title(const string& new_name)
: Name_Id(new_name)
{ };
size_t max_width(void) const
{ return 128};
};
template <typename AnyType>
struct Iterator_Functor
{
virtual void operator()(AnyType& a) = 0;
}
I have a Name_Id database table class which
contains Name_Id records. This is a base class
for other tables of this kind.
class Name_Table
{
public:
void for_each(Iterator_Functor<AnyType>& functor);
}
The for_each method applies the functor to each record
in the table.
I have a Category_Table class which represents
a database table containing Category objects:
class Singleton;
class Category_Table
: public Name_Table, public Singleton
{
public:
static Category_Table& ref(); //The singleton method.
};
I want to pass an Iterator_Functor<Category> to the
"for_each" method of the Category_Table:
Iterator_Functor<Category> functor;
Category_Table::ref().for_each(functor);
The compiler is complaining that a
Iterator_Functor<Category>
cannot be substituted for a
Iterator_Functor<Name_Id>
even though Category is-a Name_Id.
I know the compiler is correct, but is there any
work-around for this?
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
I would like to apply inheritance to a template parameter, but my design
fails to compile: cannot initialize one template class with child
child parameterized class. I'll explain...
Given:
#include <string>
// Parent class
class Name_Id
{
public:
Name_Id(const string& new_name,
int new_id = 0)
: name(new_name), id(new_id)
{}
virtual size_t max_width(void) const = 0;
// Other methods here...
private:
string name;
int id;
};
// Child classes
class Category
: public Name_Id
{
public:
Category(const string& new_name)
: Name_Id(new_name)
{ };
size_t max_width(void) const
{ return 32};
};
class Title
: public Name_Id
{
public:
Title(const string& new_name)
: Name_Id(new_name)
{ };
size_t max_width(void) const
{ return 128};
};
template <typename AnyType>
struct Iterator_Functor
{
virtual void operator()(AnyType& a) = 0;
}
I have a Name_Id database table class which
contains Name_Id records. This is a base class
for other tables of this kind.
class Name_Table
{
public:
void for_each(Iterator_Functor<AnyType>& functor);
}
The for_each method applies the functor to each record
in the table.
I have a Category_Table class which represents
a database table containing Category objects:
class Singleton;
class Category_Table
: public Name_Table, public Singleton
{
public:
static Category_Table& ref(); //The singleton method.
};
I want to pass an Iterator_Functor<Category> to the
"for_each" method of the Category_Table:
Iterator_Functor<Category> functor;
Category_Table::ref().for_each(functor);
The compiler is complaining that a
Iterator_Functor<Category>
cannot be substituted for a
Iterator_Functor<Name_Id>
even though Category is-a Name_Id.
I know the compiler is correct, but is there any
work-around for this?
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book