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
Printable View
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
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.
That's more of an inhertiance question, something likeQuote:
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
The art of writing a type independant code (generic) is, IMHO,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);
}
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.