473,796 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()(cons t Vector_t & x, Vector_t & y) const {};

protected:

};

class Affine : public Transform
{
public:

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

private:

};

Similarly other Transform derived classes.

Feb 24 '06 #1
2 2828
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()(cons t Vector_t & x, Vector_t & y) const {};

protected:

};

class Affine : public Transform
{
public:

template <class Vector_t>
void operator()(cons t 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()(cons t Vector_t & x, Vector_t & y) const {};

protected:

};

class Affine : public Transform
{
public:

template <class Vector_t>
void operator()(cons t 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(So urce, 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
6000
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"; } // Other members
8
1217
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 base class: #include <iostream> struct A {
11
3438
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 exception neutral/ I got a reply from Bux with his code for a smart pointer with far fewer lines of code and more cleaner design, not over engineered like mine. ...
7
2119
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 BufferBase, and customized in order to be able to a type of data (of any kind, such as chunks of audio samples from a sound card). The processor classes are derived from ProcessorBase, and are customized to handle BufferBase-derived objects. Each...
12
2387
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 consecutive layers of diamond-shaped inheritance; the first layer is declared as using virtual inheritance, while the second one is declared as *not* using it. This is what I'd like to have:
5
3165
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 to simulate it somehow. I read in some other posts that it is somehow possible throg visitor pattern but i m unable to get it done. My intended code is posted below. can somebody give me a workaround to get this functionality.Any help is appreciated....
14
2345
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 hash string class ) are derived. The two derived classes are template classes specifying the sizes. The base class is a non-template class so that it can be used generically in the interface classes. the design would look like
10
1471
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 static function (which I know is illegal), but, is there a way to provide something similar? The class that is the target of my inquiry is a template class that interfaces to one of several derived classes through a pointer to a base class. The...
7
7381
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 instantiated. The difference now is that I'm making a generic interface with template arguments. Template classes should be defined in the header file, but it is not allowed for a destructor's definition to be in the class definition if the...
0
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10456
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10012
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.