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

Variable to take type of templated class with variable template parameter

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.

switch (method) {
case 1:
pointer = new MyClass<1>;
break;

case 2:
pointer = new MyClass<2>;
break;

The question is: what type should 'pointer' be?

The only thing that occurs to me is to inherit MyClass from a non-
templated abstract base class:
class BaseClass {
};

template<int n>
class MyClass : public BaseClass {
};

and then define
BaseClass* pointer;

This seems to work, but it requires me to define dummy pure virtual
versions in BaseClass of all the methods in MyClass, which seems like
something that should be able to happen automatically.

Is there a simpler / better solution to this problem?

[The other question is how to make the switch statement automatic, but
that I guess one does with some kind of factory method?]

Thanks and best wishes,
David.

Oct 29 '07 #1
4 2339
On Oct 29, 8:08 am, David Sanders <dpsand...@gmail.comwrote:
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.
switch (method) {
case 1:
pointer = new MyClass<1>;
break;
case 2:
pointer = new MyClass<2>;
break;
The question is: what type should 'pointer' be?
The only thing that occurs to me is to inherit MyClass from a non-
templated abstract base class:
class BaseClass {
};
template<int n>
class MyClass : public BaseClass {
};
and then define
BaseClass* pointer;
This seems to work,
It's also about the only thing that will work. Different
instantiations of MyClass are unrelated classes; you have to
explicitly relate them.
but it requires me to define dummy pure virtual versions in
BaseClass of all the methods in MyClass, which seems like
something that should be able to happen automatically.
Not really. What should this "implicitly defined" base class
look like if there are specializations of your template?
Templates and runtime polymorphism are two very different
things. If you want runtime polymorphism, you must follow the
rules of runtime polymorphism (even if you use templates in the
implementation of the derived types).

The basic reason for this is that runtime polymorphism takes
place in a different environment than template instantiation.
Templates can get away with duck typing because if you
instantiate over a type which doesn't meet the contract, the
compiler will complain, and the error doesn't go any further.
Use duck typing a runtime (a la Smalltalk or Lisp), and you risk
type errors at runtime---require a common base class and
explicit derivation, and the compiler can catch most of these
errors.
Is there a simpler / better solution to this problem?
[The other question is how to make the switch statement
automatic, but that I guess one does with some kind of factory
method?]
More or less (although I usually use factory objects). Just put
the address of the function/object in a map, indexed by the key.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 29 '07 #2
David Sanders wrote:
Hi,

I have a class depending on a template parameter:
...
Is there a simpler / better solution to this problem?
No, there isn't. Different instantiation of template classes are not
related anyhow, so the correct way to link them is in letting them
derive from a single base class. And, the virtual functions are
unavoidable if you want polymorphic behaviour.
[The other question is how to make the switch statement automatic, but
that I guess one does with some kind of factory method?]
The switch part has to appear somewhere (in the factory, possibly),
since the template argument has to be resolved at compile-time. However,
depending on your specific problem you can rely on some class
auto-registration mechanism.
Thanks and best wishes,
David.
Best regards,

Zeppe

Oct 29 '07 #3
On Oct 29, 10:08 am, David Sanders <dpsand...@gmail.comwrote:
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.

switch (method) {
case 1:
pointer = new MyClass<1>;
break;

case 2:
pointer = new MyClass<2>;
break;

The question is: what type should 'pointer' be?

The only thing that occurs to me is to inherit MyClass from a non-
templated abstract base class:

class BaseClass {

};

template<int n>
class MyClass : public BaseClass {

};

and then define
BaseClass* pointer;

This seems to work, but it requires me to define dummy pure virtual
versions in BaseClass of all the methods in MyClass, which seems like
something that should be able to happen automatically.

Is there a simpler / better solution to this problem?

[The other question is how to make the switch statement automatic, but
that I guess one does with some kind of factory method?]

Thanks and best wishes,
David.
void * pointer;/*you will be responsible for type of the object*/

if 'pointer' is not of a polymorphic type you will be in trouble ,so I
think this is really bad.

regards,
FM.

regards,
FM.

Oct 29 '07 #4
On Oct 29, 3:36 am, Zeppe
<ze...@remove.all.this.long.comment.yahoo.itwrot e:
David Sanders wrote:
Hi,
I have a class depending on a template parameter:

...
Is there a simpler / better solution to this problem?

No, there isn't. Different instantiation of template classes are not
related anyhow, so the correct way to link them is in letting them
derive from a single base class. And, the virtual functions are
unavoidable if you want polymorphic behaviour.
[The other question is how to make the switch statement automatic, but
that I guess one does with some kind of factory method?]

The switch part has to appear somewhere (in the factory, possibly),
since the template argument has to be resolved at compile-time. However,
depending on your specific problem you can rely on some class
auto-registration mechanism.
Hi, thanks very much for the helpful replies. I'm happy that I was on
the right track!

Best wishes,
David.

Oct 30 '07 #5

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

Similar topics

2
by: John Harrison | last post by:
I want to write a templated constructor with a non-type template argument, like this. class X { public: X() : val(0) {} template <int I> X(X const&, X const&) : val(I) {}
7
by: bartek | last post by:
Hello, I've been pondering with this for quite some time now, and finally decided to ask here for suggestions. I'm kind of confused, actually... Maybe I'm thinking too much... Brain dump...
3
by: Steve Brown | last post by:
Hello all, Is there a way to determine a variable's type at run-time? The reason I'm asking is that i have code that looks like this: template <class T> Object::Object(int TypeCode, T* data)...
3
by: case2005 | last post by:
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename...
0
by: Chris Jefferson | last post by:
Now, C++ doesn't actually provide true variable-length templated classes, for example you can't do (as I found recently!) template<class T> class tuple { ... } template<class T, class U>...
16
by: frs | last post by:
See example below: Why does the output of 'a' work and the output of 'b' fails to compile? Is there a way to write class 'something' so that 'b' converts correctly by default? (include iostream,...
2
by: Alex Drummond | last post by:
Hello, Is there any way of specializing a templated function on a type which is itself templated? Here's the simplest example of the problem I can think of. Say I have written an implementation...
2
by: Pete C | last post by:
Hello... I am writing a templated 'wrapper' class that takes as its template parameter a class to be inherited from. I need a specialisation of this wrapper class in certain cases, however I am...
2
by: Pierre Yves | last post by:
Hi there, Sorry for the double subject but I feel they are related. I'm not pretty sure there would be an answer but I reckon there must be a way to make it work. I would like to write the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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.