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

    template formal parameter

    Hi,
    How can I define template formal parameter as T*?

    I read in some textbook that if I define template with formal parameter T* then
    actual parameter can be of the type T1*,provided that T1 is derived from T

    Thank you in advance

  2. #2
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: template formal parameter

    Quote Originally Posted by bil050 View Post
    Hi,
    How can I define template formal parameter as T*?
    pretty much the same way you would with non-template functions.
    the parameters you provide don't really have any intrinsic meanings,
    and since templates can take the parameters of ordinary types, type parameters, and template parameters,
    the only meaningful value of the template parameters is to distinguish among those.
    I read in some textbook that if I define template with formal parameter T* then
    actual parameter can be of the type T1*,provided that T1 is derived from T
    That's more of an inhertiance question, something like
    Code:
    #include <iostream>
    
    // sample classes
    class Foo
    {
    public:
        virtual ~Foo() {}
        virtual void say() const
        {
            std::cout << "Foo says what's the deal?";
        }
    };
    
    class Bar : public Foo
    {
    public:
        void say() const
        {
            std::cout << "Bar says what's the deal?";
        }
    };
    
    // test
    template <typename FooClassPlease>
    void iAskedForAFooPointer(const FooClassPlease* ptr)
    {
        ptr->say();
    }
    
    int main()
    {
        Bar bar;
        Bar* butYouPassABarPointer = &bar; //static
        iAskedForAFooPointer(butYouPassABarPointer);
    }
    The art of writing a type independant code (generic) is, IMHO,
    much harder than learning the basics of polymorphism.
    What I mean is that your second question should have been the first to study for.

    Hope this helps.
    Last edited by potatoCode; September 14th, 2009 at 04:32 AM.

Tags for this Thread

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