473,387 Members | 1,844 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,387 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 2788
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.