473,394 Members | 1,714 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,394 software developers and data experts.

template and dynamic polymorphism

Jun
struct Abstract{

virtual void run() const = 0;

};

template <typename T>
struct S : F{
void run(){}
};
I just want to use as :

Abstract* a = new S<policy_1>(); // doesn't work
I just wanna to know how i could mix the templates with the
traditional abstract class design, thank you in advance.
Aug 11 '08 #1
5 1293
Jun
Sorry, the code is :

struct Abstract{

* * virtual void run() const = 0;

};

template <typename T>
struct S : Abstract{
* * void *run(){}

};

I just want to use as :

Abstract* a = new S<policy_1>(); // doesn't work

I just wanna to know how i could mix the templates with the
traditional abstract class design, thank you in advance.
Aug 11 '08 #2
On 11 ago, 17:09, Jun <junh...@gmail.comwrote:
Sorry, the code is :
struct Abstract{
* * virtual void run() const = 0;
};
template <typename T>
struct S : Abstract{
* * void *run(){}
};
I just want to use as :
Abstract* a = new S<policy_1>(); // doesn't work
I just wanna to know how i could mix the templates with the
traditional abstract class design, thank you in advance.

You get the error because you're hidding run() from class abstract (in
S you're not making the function const). Try this:
template <typename T>
struct S : Abstract
{
void run()const {} //Notice the const at the end.
};

Aug 11 '08 #3
Alf P. Steinbach wrote:
* Jun:
>struct Abstract{

virtual void run() const = 0;

};

template <typename T>
struct S : F{
void run(){}
};
I just want to use as :

Abstract* a = new S<policy_1>(); // doesn't work
I just wanna to know how i could mix the templates with the
traditional abstract class design, thank you in advance.

How about deriving S from Abstract.
Another thing that comes to mind would be to make run() in S const, like in
Abstract, as well as telling, what "doesn't work" exactly means in this
case.
Aug 11 '08 #4
Jun
On Aug 11, 10:14*pm, Rolf Magnus <ramag...@t-online.dewrote:
Alf P. Steinbach wrote:
* Jun:
struct Abstract{
* * virtual void run() const = 0;
};
template <typename T>
struct S : F{
* * void *run(){}
};
I just want to use as :
Abstract* a = new S<policy_1>(); // doesn't work
I just wanna to know how i could mix the templates with the
traditional abstract class design, thank you in advance.
How about deriving S from Abstract.

Another thing that comes to mind would be to make run() in S const, like in
Abstract, as well as telling, what "doesn't work" exactly means in this
case.
Thanks a lot, after changing the run as const in S, it works fine.

Aug 11 '08 #5
Jun wrote:
On Aug 11, 10:14 pm, Rolf Magnus <ramag...@t-online.dewrote:
>Alf P. Steinbach wrote:
>>* Jun:
struct Abstract{
virtual void run() const = 0;
};
template <typename T>
struct S : F{
void run(){}
};
I just want to use as :
Abstract* a = new S<policy_1>(); // doesn't work
I just wanna to know how i could mix the templates with the
traditional abstract class design, thank you in advance.
How about deriving S from Abstract.
Another thing that comes to mind would be to make run() in S const, like in
Abstract, as well as telling, what "doesn't work" exactly means in this
case.

Thanks a lot, after changing the run as const in S, it works fine.
This will repeatedly mess with you. Make it the first thing you check.
Aug 12 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
6
by: Marcel Sebbao | last post by:
I have a function that returns {sin(x),x,cos(x)} depending on a parameter k that can take only 3 integer values: -1, 0, 1. I am trying to do this with template specialization: enum curv {...
6
by: MikeY | last post by:
Hi Everyone, Does anyone know where I can get my hands on a sample with source code of a simple dynamic button control in C# Windows form. I am looking for a sample that uses a class library...
3
by: Raider | last post by:
I'm trying to create a base class who will contain generic algoritms and derived class(es) who will perform problem-specific things. I'm unable to use dynamic polymorphism, because base class know...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
5
by: Jon Rea | last post by:
I work with molecular simulations. In a normal infinate space simulation, a particle can occupy any cartesian position in space. If i want the distance between two particles in a system I can...
4
by: chsalvia | last post by:
The "Curiously Recurring Template Pattern" (CRTP) recently caught my attention as a nice trick which allows static polymorphism. In other words, it lets you build extensible classes without the...
4
by: David Sanders | last post by:
Hi, I have a class depending on a template parameter: template<int n> class MyClass { }; I want the template parameter to be chosen according to a variable, e.g.
8
by: Belebele | last post by:
Suppose that I want to write a (concrete) interface class (without virtual functions) to classes that contain a method template // -------------------------------------- class Interface {...
8
by: Ralf Goertz | last post by:
Hi, I want to do able to declare an object bar of a templated class foo where the actual template type of foo is only know at runtime: ----- #include <string> template <class Tclass foo {
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.