Abstract Classes, Templates and inheritance...

Discussion in 'C++' started by ptkacz@cogeco.ca, Aug 6, 2005.

  1. Guest

    I've got two classes, B and C, each performing essentially the same
    functionality, except for the fact that they each operate on a different
    data type, and that they each have a class method that has different logic
    to work with the respective datatype. I was thinking that I'd create an
    abstract template class A, that would be inhereted by both B and C classes.
    All that would be left to do then, is implement a single method in B and C,
    saving lots of work (except for the headics). My question is, how in
    classes B and C, when each inherits A, how does one specify a type to A?

    I might have something like:

    template <class T>
    class A
    {
    public:
    A();
    void commonMethod() {... common code ... };
    virtual void notSoCommonMethod() = 0;

    private:
    T* var;
    };


    class B : public A
    {
    public:
    B() : A<some type>();

    void notSoCommonMethod() { ... code working on <some type> ... return; }
    };

    class C : public A
    {
    public:
    C() : A<another type>();

    void notSoCommonMethod() { ... code working on <another type> ...
    return; }
    };

    I could then just code:

    B varB;
    C varC;

    varB.commonMethod();
    varC.commonMethod();

    varB.notSoCommonMethod();
    varC.notSoCommonMethod();

    Have I just answered my own question?

    Peter
     
    , Aug 6, 2005
    #1
    1. Advertising

  2. sade:
    > saving lots of work (except for the headics). My question is, how in
    > classes B and C, when each inherits A, how does one specify a type to A?
    >

    [snip]
    >
    >
    > class B : public A
    > {
    > public:
    > B() : A<some type>();
    >


    class B : public A<char>
    {
    public:
    B();

    Tobias
    --
    IMPORTANT: The contents of this email and attachments are confidential
    and may be subject to legal privilege and/or protected by copyright.
    Copying or communicating any part of it to others is prohibited and may
    be unlawful.
     
    Tobias Blomkvist, Aug 6, 2005
    #2
    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:
    557
    Lee Weiner
    Apr 20, 2004
  2. JKop
    Replies:
    3
    Views:
    492
  3. Tony Johansson

    Inheritance and abstract classes

    Tony Johansson, Apr 27, 2005, in forum: C++
    Replies:
    4
    Views:
    445
    Cy Edmunds
    Apr 28, 2005
  4. Dan Smithers

    templates and abstract base classes

    Dan Smithers, Jun 19, 2008, in forum: C++
    Replies:
    4
    Views:
    420
    Dan Smithers
    Jun 20, 2008
  5. Barzo
    Replies:
    3
    Views:
    263
    Victor Bazarov
    Oct 14, 2009
Loading...

Share This Page