473,216 Members | 1,292 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 software developers and data experts.

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 2778
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: qazmlp | last post by:
My class in a header file, contains inline virtual destructor. Is this Ok? Can it cause any problems? class base { public: base() { } virtual ~base { std::cout<<"Inside virtual destructor\n";...
8
by: Pete Vidler | last post by:
Hi folks, I have a small hierarchy with a virtual method which is overridden in the most derived class. I also have a method that needs to call two versions of this.. the most derived and the...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
12
by: Massimo | last post by:
Hi to all, I'm facing a problem in a particularly complex inheritance hierarchy, and I'd like to know what the standard says about it and if my compiler is correct in what it does. I have two...
5
by: alind | last post by:
I m having a problem with templates. A per C++ std says i cant use override template virtual functions. in other words virtual functions cant be templates due to some vtable size issues. Now i want...
14
by: Hunk | last post by:
Hi I ws wondering if there is a way to implement operator+ in case of virtual classes. Here's the problem. I have to have a base string class from which two classes (normal char string and a...
10
by: =?Utf-8?B?Y2FybG0=?= | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual...
7
by: Tonni Tielens | last post by:
I'm trying to create a pure virtual class describing an interface. Normally, when I do this I make the destructor pure virtual so that, even if there are no members in the class, it cannot be...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.