Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   templates and abstract base classes (http://www.velocityreviews.com/forums/t621180-templates-and-abstract-base-classes.html)

Dan Smithers 06-19-2008 02:20 PM

templates and abstract base classes
 
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?

joseph cook 06-19-2008 05:21 PM

Re: templates and abstract base classes
 
On Jun 19, 10:20*am, Dan Smithers <dsmith...@talktalk.net> wrote:
> Is it possible to realise an abstract base class with a template?


Of course. Didn't you try it?

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


Most certainly.


Road.Tang 06-20-2008 07:34 AM

Re: templates and abstract base classes
 
On Jun 19, 10:20 pm, Dan Smithers <dsmith...@talktalk.net> wrote:
> 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

Dan Smithers 06-20-2008 08:07 AM

Re: templates and abstract base classes
 
joseph cook wrote:
> On Jun 19, 10:20 am, Dan Smithers <dsmith...@talktalk.net> wrote:
>> Is it possible to realise an abstract base class with a template?

>
> 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.

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

>
> Most certainly.
>


Dan Smithers 06-20-2008 08:11 AM

Re: templates and abstract base classes
 
Thanks for your reply

Road.Tang wrote:
>
> 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


All times are GMT. The time now is 11:36 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.