-
September 13th, 2009, 08:03 AM
#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
-
September 14th, 2009, 04:27 AM
#2
Re: template formal parameter
Originally Posted by bil050
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|