Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

virtual template function

6 views
Skip to first unread message

Sebastian Faust

unread,
Jan 3, 2003, 11:32:46 AM1/3/03
to
Hi,

I know, that it is not possible to create virtual template functions. If I
wanna derive from a class which has a template function and that function
usually should been overwritten then how can I solve this problem?
Here is a piece of sample code:

class Base
{
template<class T>
virtual bool function(const T& t)
{
return true;
}
};

class Derived : public Base
{
template<class T>
virtual bool function(const T& t)
{
return false;
}
};

How can I solve this problem, that overwritting the function "function"?

Thanks in advance
Sebastian


Victor Bazarov

unread,
Jan 3, 2003, 11:44:46 AM1/3/03
to
"Sebastian Faust" <sfa...@askme.de> wrote...

> Hi,
>
> I know, that it is not possible to create virtual template functions. If I
> wanna derive from a class which has a template function and that function
> usually should been overwritten then how can I solve this problem?

First of all, the term is "overridden".

> Here is a piece of sample code:
>
> class Base
> {

public:

> template<class T>
> virtual bool function(const T& t)

bool function(const T& t) // no "virtual"

> {
> return true;
> }
> };
>
> class Derived : public Base
> {

public:

> template<class T>
> virtual bool function(const T& t)

bool function(const T& t) // no "virtual"

> {
> return false;
> }
> };
>
> How can I solve this problem, that overwritting the function "function"?

What problem? You didn't say what you're trying to _achieve_.

In your code Derived::function<> _hides_ Base::function<>. Is
it NOT what you want? If you say

Derived d;
bool b = d.function(d);

'b' will get value 'false'.

IOW, tell us what it is you're trying to _do_. Don't tell us
the non-working solution you have already "found".

Victor
--
Please remove capital A's from my address when replying by mail


Claudio Puviani

unread,
Jan 3, 2003, 11:52:59 AM1/3/03
to
"Sebastian Faust" <sfa...@askme.de> wrote

Make the class a template and not the function, like so:

template<class T>
class Base
{


virtual bool function(const T& t)
{
return true;
}
};

template<class T>
class Derived : public Base<T>


{
virtual bool function(const T& t)
{
return false;
}
};

That way, for each template instantiation, you still have a fixed and
well-defined set of virtual methods (which is the reason for disallowing
template virtual methods).

Claudio Puviani


tom_usenet

unread,
Jan 3, 2003, 11:58:29 AM1/3/03
to
On Fri, 3 Jan 2003 17:32:46 +0100, "Sebastian Faust" <sfa...@askme.de>
wrote:

return do_function();
}

virtual bool do_function()
{
return true;
}
};

class Derived : public Base
{
virtual bool do_function()
{
return false;
}
};

I've found that the principle above can be applied to most situations.
Usually, the thing that you want to vary depending on the dynamic type
isn't dependent on the template parameter, and can be factored out
into one or more virtual functions. I've used the above construct in a
couple of class heirarchies in the past.

I'd call this pattern the "Template Template Method", like the GOF's
"Template Method", but with templates!

Tom

0 new messages