templates and abstract base classes

Discussion in 'C++' started by Dan Smithers, Jun 19, 2008.

  1. Dan Smithers

    Dan Smithers Guest

    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?
    Dan Smithers, Jun 19, 2008
    #1
    1. Advertising

  2. Dan Smithers

    joseph cook Guest

    On Jun 19, 10:20 am, Dan Smithers <> 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.
    joseph cook, Jun 19, 2008
    #2
    1. Advertising

  3. Dan Smithers

    Road.Tang Guest

    On Jun 19, 10:20 pm, Dan Smithers <> 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
    Road.Tang, Jun 20, 2008
    #3
  4. Dan Smithers

    Dan Smithers Guest

    joseph cook wrote:
    > On Jun 19, 10:20 am, Dan Smithers <> 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, Jun 20, 2008
    #4
  5. Dan Smithers

    Dan Smithers Guest

    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
    Dan Smithers, Jun 20, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DaKoadMunky
    Replies:
    4
    Views:
    547
    Lee Weiner
    Apr 20, 2004
  2. John Gagon
    Replies:
    1
    Views:
    367
    LordAlfredHenry
    May 17, 2006
  3. Replies:
    1
    Views:
    322
    Tobias Blomkvist
    Aug 6, 2005
  4. Bart Simpson
    Replies:
    4
    Views:
    308
    James Kanze
    Jun 3, 2007
  5. Vinesh S
    Replies:
    4
    Views:
    410
    Vinesh S
    Aug 22, 2011
Loading...

Share This Page