470,848 Members | 1,709 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

virtual template members

Template member functions cannot be virtual - I know - but is there any
way to simulate that (other than making the entire class templated,
with non-templated members).
What I have is this:

class Transform
{
public:

// Each derived Transform should compute y=Transform(x)
// in its own way, so this operator() should be virtual,
// but I also want it to be templated on the vector type.
template <class Vector_t>
void operator()(const Vector_t & x, Vector_t & y) const {};

protected:

};

class Affine : public Transform
{
public:

template <class Vector_t>
void operator()(const Vector_t & x, Vector_t & y) const {
// real calculation of y=Affine(x).
}

private:

};

Similarly other Transform derived classes.

Feb 24 '06 #1
2 2625
Amadeus W. M. wrote:
Template member functions cannot be virtual - I know - but is there
any way to simulate that (other than making the entire class
templated, with non-templated members).
What I have is this:

class Transform
{
public:

// Each derived Transform should compute y=Transform(x)
// in its own way, so this operator() should be virtual,
// but I also want it to be templated on the vector type.
template <class Vector_t>
void operator()(const Vector_t & x, Vector_t & y) const {};

protected:

};

class Affine : public Transform
{
public:

template <class Vector_t>
void operator()(const Vector_t & x, Vector_t & y) const {
// real calculation of y=Affine(x).
}

private:

};

Similarly other Transform derived classes.


Of course, I don't know for sure, but it seems that you're misusing
templates. The whole idea behind the generic programming is that types
and/or values for which the structures and/or algorithms are created
are _totally_ unrelated in C++ sense. The only thing that unifies
those types or values are their _traits_. For example, iterators can
be incremented using ++. Numbers can be added or multiplied. And so
on.

In your case I am questioning the relationship you have established
between "Transform" and "Affine" (and "other Transform derived classes")
while totally disconnecting the "vector" type they are about to work on.

If 'Affine' and other derived types operate on some vectors, are those
really totally unrelated vectors? Why then the Transforms themselves
are not templates? Could it be that you've not separated your system
abstractions enough?

V
--
Please remove capital As from my address when replying by mail
Feb 24 '06 #2
On Thu, 23 Feb 2006 21:47:43 -0500, Victor Bazarov wrote:
Amadeus W. M. wrote:
Template member functions cannot be virtual - I know - but is there
any way to simulate that (other than making the entire class
templated, with non-templated members).
What I have is this:

class Transform
{
public:

// Each derived Transform should compute y=Transform(x)
// in its own way, so this operator() should be virtual,
// but I also want it to be templated on the vector type.
template <class Vector_t>
void operator()(const Vector_t & x, Vector_t & y) const {};

protected:

};

class Affine : public Transform
{
public:

template <class Vector_t>
void operator()(const Vector_t & x, Vector_t & y) const {
// real calculation of y=Affine(x).
}

private:

};

Similarly other Transform derived classes.


Of course, I don't know for sure, but it seems that you're misusing
templates. The whole idea behind the generic programming is that types
and/or values for which the structures and/or algorithms are created
are _totally_ unrelated in C++ sense. The only thing that unifies
those types or values are their _traits_. For example, iterators can
be incremented using ++. Numbers can be added or multiplied. And so
on.

In your case I am questioning the relationship you have established
between "Transform" and "Affine" (and "other Transform derived classes")
while totally disconnecting the "vector" type they are about to work on.

If 'Affine' and other derived types operate on some vectors, are those
really totally unrelated vectors? Why then the Transforms themselves
are not templates? Could it be that you've not separated your system
abstractions enough?

V


Well, everything is part of a bigger picture, which I haven't described.
What connects the base Transform and the derived concrete Affine,
Similarity, etc. is a mathematical operation called registration. Given
two vectors X and Y of the same size, find the best transform T (within
one of the derived categories), which aligns X to Y (in the mean-squared
sense), i. e. find the transform for which T(xi) ~ yi for all i.
Here X=(xi) and Y=(yi) are complex vectors. The actual T is computed
differently, depending on its type.

Each derived class computes its own parameters within a

virtual void registration(Source, Target)

member. Once it does, I want to be able to apply the operator()(X,Y) to
vectors of possibly different types, other than the types for which the
registration was done. I really need this. There may be a design flaw
somewhere else, but the project this is part of is too big to re-design.

So I think the registration does warrant a hierarchy, because I do need an
abstract Transform class, but it would be good if operator() was templated
(and virtual).
Feb 24 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by qazmlp | last post: by
8 posts views Thread by Pete Vidler | last post: by
7 posts views Thread by Markus Svilans | last post: by
12 posts views Thread by Massimo | last post: by
5 posts views Thread by alind | last post: by
14 posts views Thread by Hunk | last post: by
10 posts views Thread by =?Utf-8?B?Y2FybG0=?= | last post: by
7 posts views Thread by Tonni Tielens | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.