Abstract Base Class template

Hi Everyone,

I'm wanting to ask if it's considered bad practice to define pure virtual methods in a class template, when that class template is to be used as an interface ("contract") for derived classes.

To use a concrete example, I'm trying to write an interface for mapper objects which will load and save objects in the database:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template <typename EntityType>
class entity_mapper
{
    public:
        virtual void create(const EntityType&)=0;

    private:
        //database connection etc
}

//a possible derived class:
class personmapper : public entitymapper<person>
{
    public:
        create(const person& p);
}


Is this good practice, or are there better, preferred ways to achieve this?

Thanks very much in advance.

All the best,
NwN
it seems like if your going to make pure virtual functions there is not a huge need for a template because in the derived class you could store whatever type is specific to that mapper (ie personmapper takes care of handing the person type)

remember pure virtual means that the base class itself can't be used - you have to derive a class

if there is something the entity_mapper needs to do with the templated type you are probably going to have some pretty case specific code based on what type its given but I don't know that really depends on the types you are planning on using with the class

you could take a look at some libraries and see how they structure their inheritance and templated classes - I have used Qt a ton and I don't remember ever seeing a templated type that had pure virtual functions.. or even virtual functions for that matter.

But anyways simplest solution is the best solution
Last edited on
Hi there,

dreamincolor wrote:
it seems like if your going to make pure virtual functions there is not a huge need for a template because in the derived class you could store whatever type is specific to that mapper (ie personmapper takes care of handing the person type)


That's mostly what prompted me to ask this question, I think I'm getting confused here.
Could you please give an example of what you mean?

It seems to me that I can't do this however:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class entity_mapper
{
    public:
        virtual void create()=0;

    private:
        //database connection etc
}

class personmapper : public entitymapper
{
    public:
        create(const person& p);
}


That's not correctly defining the pure virtual is it, because it takes a different argument?
Thanks for your reply though, greatly appreciated.

All the best,
NwN
Last edited on
> ask if it's considered bad practice to define pure virtual methods in a class template,
> when that class template is to be used as an interface ("contract") for derived classes.

No.

What you are aiming at is compile-time polymorphic behaviour across types (EntityType in this case), along with run-time substitutability of different implementations for the same type. Typically, a template specialization would be defined for each different EntityType; and these specializations can be customized via inheritance and overriding.

Typical example in the standard library are the different facet types;
for instance the specializations of std::numpunct<> http://en.cppreference.com/w/cpp/locale/numpunct

Last edited on
Thanks very much JLBorges, that was exactly what I was after.

Only, after learning (from yourself, on a sidenote) about compile time polymorphism and run time polymorphism I started to get confused in this particular case.

You are exactly right about the intention, I will have a look at the STL examples you provided and have a look at their implementations. If you remember my question on the crudl class, this is how I'm solving that problem, by using mapper classes the database behaviour is decoupled and can be replaced by a custom mapper class (which can use XML or JSON for example) if necessary.

Thanks again, you've been a great help.

All the best,
NwN
Topic archived. No new replies allowed.