templates and abstract base classes

D

Dan Smithers

Is it possible to realise an abstract base class with a template? The
reason that I'm considering this is that I would like to present a fixed
interface that I realise using a completely independent implementation
module and avoid including the template code in the client code.

I want something like this:

interface.

ILog
{
public:
virtual void method() = 0;
virtual ~ILog;
static ILog& create();
protected:
ILog();
};

realisation:

template <typename T>
class CLog:ILog
{
public:
virtual void method();
virtual ~CLog;
static CLog& create();
protected:
CLog();
}

Am I actually trying to make this more complicated than it needs to be?
 
R

Road.Tang

Is it possible to realise an abstract base class with a template?

yes, after template class is instanced, it's the same as the common
class.
The reason that I'm considering this is that I would like to present a fixed
interface that I realise using a completely independent implementation
module and avoid including the template code in the client code.

I'm afraid you can't avoid to #include template code.
since you must instance the template class CLog<T> , at the instance
place, it must see the template definition. (the template export is
still not supported well in mainstream compilers).

but yes, use ILog, the client can be constructed using ILog interface,
i.e.
the client C++ programmer can work with little template knowledge,.
I want something like this:

interface.

ILog
{
public:
virtual void method() = 0;
virtual ~ILog;
static ILog& create();
protected:
ILog();

};

realisation:

template <typename T>
class CLog:ILog
{
public:
virtual void method();
virtual ~CLog;
static CLog& create();
protected:
CLog();

}

Am I actually trying to make this more complicated than it needs to be?

It depends what you need.

ILog can transfer template-form code into interface-form.
As mentioned, if client programmer doesn't like or know the template,
ILog (interface adaptor) is important.

-roadt
 
D

Dan Smithers

joseph said:
Of course. Didn't you try it?

yes, but it didn't work. I thought asking a quick question first would
be better than spending a lot of time not getting it to work and then
asking the question.
 
D

Dan Smithers

Thanks for your reply

Road.Tang said:
It depends what you need.

ILog can transfer template-form code into interface-form.
As mentioned, if client programmer doesn't like or know the template,
ILog (interface adaptor) is important.

That's what I'm hoping to do. I want to write a library that conforms to
the interface that the client can use without knowing what is being used
to implement it. The template code would be included inside the library
and hidden from the client.

thanks

dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top