CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2009
    Posts
    7

    Templates and inheritance

    Here's a templated parent class:

    Code:
    template<class T>
    class A
    {
    public:
       A(T param);
       ~A();
    }
    
    template<class T>
    A::A(T param)
    {
       // do stuff with type T parameter
    }
    
    template<class T>
    A::~A()
    {
       // undo stuff
    }
    I want to inherit this class with a specific type, like this:

    Code:
    class B : public A<int>   // or any other type
    {
    public:
       B(int param);
       ~B();
    }
    
    B::B(int param) : A(param)
    {
    
    }
    
    B::~B()
    {
    }
    Is this possible? Is this the right syntax to do it?

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Templates and inheritance

    Looks right.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  3. #3
    Join Date
    Mar 2008
    Posts
    38

    Re: Templates and inheritance

    It is definitely possible. Syntax looks okay. I usually typedef template instantiations.
    Code:
    typedef A<int> AInstantiation;
    
    class B : public AInstantiation
    Not sure if it makes a difference.

    Also, from my limited experience templates and inheritance are exclusive (not in the standard but in the practice). Do you really want inheritance?

  4. #4
    Join Date
    Nov 2006
    Posts
    1,611

    Re: Templates and inheritance

    It was common in the first years of templates to derive a template from a 'standard' class, but the reverse was discouraged from worries of code bloat. The real issue back then was that 64 MBytes of RAM was about $1,000, and compilers would choke on template code.

    In modern work you will find template bases to templates, which themselves are part of libraries you may be expected to derive "standard" classes from, very much like you're doing here.

    It can be a means of introducing traits or other compile time modulations of the base behavior. You can think of the base template as an interface, where the type supplied to it may be an implementation. There are many other uses.

    You may be ready to dive into Alexandrescu's (sp?) book on "Modern C++ Design - Generic Programming and Design Patterns Applied", and/or any of the "Exceptional C++" series, as they discuss these notions.

    Do you really want inheritance?
    It's certainly a valid question.

    The reason for construction of this type may be that the base represents part of a library you're creating, and the type supplies a trait or behavior selection at compile time, which traditionally may have been part of some parameter provided during base class construction.

    In a variation on this idea, you'll find that string is a typedef for a class which accepts traits and is derived from a template class in much the same way you provide here. The purpose is to allow string to operate as a 'common' ASCII type string, or a unicode "wide character" string by selecting the trait.

    The issue comes to mind about the requirements of doing this. The base class will be a type relative to the parameter (int in your example), which means you're not going to be 'storing' these things in a container based on that expression - you want that to remain anonymous, which hints that the inheritance might not be public. Do you want to have to provide a virtual destructor? Do you need inheritance from the template, or is the standard class a "consumer" of this template? (the classic Is-a or Has-a question).
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  5. #5
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Templates and inheritance

    The reason for construction of this type may be that the base represents part of a library you're creating, and the type supplies a trait or behavior selection at compile time, which traditionally may have been part of some parameter provided during base class construction.

    In a variation on this idea, you'll find that string is a typedef for a class which accepts traits and is derived from a template class in much the same way you provide here. The purpose is to allow string to operate as a 'common' ASCII type string, or a unicode "wide character" string by selecting the trait.
    Few months ago, i have developed a Graph template that accept traits at CT which can operate on directed or undirected graph.

    The behaviour selection is policy class.

    Not sure how string was implemented.
    Thanks for your help.

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Templates and inheritance

    Code:
    class B : public A<int>
    I've used this technique a lot in my GUI & image library to create customised concrete types from a generic interface template.

    It can also be an alternative method of getting around multiple inheritance problems, where you supply the class to derive from as the template parameter.

    Code:
    tempate <typename T>
    class Window : public T
    {
        // Common helper functions.
    };
    
    class Special_Edit_Box : public Window<CEdit>
    {
        // Edit box functions.
    };
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured