Hi all,
I have a question which is illustrated by the following piece of code:
template <class T>
class A {
T my_value;
};
In a list, I'd like to store pointers to objects of class A. But I don't
know at forehand if these will be A<intor A<doubleor whatever (all
these flavours of A must be stored in the same list). And, lateron if I
retrieve the objects from the list I may have to know what flavour the
object was in order to call the proper routines for processing them.
My question: what does the list look like in C++? I'm lost, so if anyone
has some advice....
Thanks,
Jeroen
Mar 6 '07
16 3194
John Harrison wrote:
You need to store all different types in the same list? And later you
need to recover what was the type of object stored in the list?
Sorry but in C++ that can't be done.
Tell that to the author of boost::any.
Noah Roberts wrote:
John Harrison wrote:
>You need to store all different types in the same list? And later you need to recover what was the type of object stored in the list?
Sorry but in C++ that can't be done.
Tell that to the author of boost::any.
Boost::any does not allow you to 'recover what was the type of object
stored'. Sure you can say, was it an int? was it a double? etc. But in
the absense of any clue (like a fixed list of possibilities) you cannot
tell what the type was.
john
On 3ÔÂ7ÈÕ, ÉÏÎç2ʱ43·Ö, Jeroen <no_m...@please .com>wrote:
Hi all,
I have a question which is illustrated by the following piece of code:
template <class T>
class A {
T my_value;
};
In a list, I'd like to store pointers to objects of class A. But I don't
know at forehand if these will be A<intor A<doubleor whatever (all
these flavours of A must be stored in the same list). And, lateron if I
retrieve the objects from the list I may have to know what flavour the
object was in order to call the proper routines for processing them.
My question: what does the list look like in C++? I'm lost, so if anyone
has some advice....
Thanks,
Jeroen
I suggest use the boost::variant.
On other way , you can do like this:
Plan A..
struct Type_Base
{
virtual std::string type_name() = 0;
virtual void * adress() = 0;
};
template <typename T>
class MyType : Type_Base
{
public:
std::string type_name() { return type_id(T).name (); }
void * address(){ return (void *)(&value);}
private:
T value;
}
Anyway boost::variant is better then paln A,but it only support
arithmetic type. You can read the document for detail.
Jeroen schreef:
John Harrison wrote:
OK guys, you all gave me a lot of pointers to think about. It will keep
me busy for some time to sort everything out. Thanks again!
Jeroen wrote:
>
I have a question which is illustrated by the following piece of code:
template <class T>
class A {
T my_value;
};
In a list, I'd like to store pointers to objects of class A. But I don't
know at forehand if these will be A<intor A<doubleor whatever (all
these flavours of A must be stored in the same list). And, lateron if I
retrieve the objects from the list I may have to know what flavour the
object was in order to call the proper routines for processing them.
My question: what does the list look like in C++? I'm lost, so if anyone
has some advice....
Design pattern "adapter" can be used to adapt templated class into virtual
base interface.
class Base
{
public:
virtual void method()=0;
virtual ~Base(){}
};
template<class T>
class A_Base: public Base
{
A<T data;
public:
void method(){ data.method(); }
};
Multiple ingeritance also can be used
template<class T>
class A_Base: public Base, public A<T>
{
public:
void method(){ A<T>::method() ; }
};
--
Maksim A. Polyanin http://grizlyk1.narod.ru/cpp_new
"In thi world of fairy tales rolls are liked olso"
/Gnume/
On Mar 7, 3:43 am, Jeroen <no_m...@please .comwrote:
Hi all,
I have a question which is illustrated by the following piece of code:
template <class T>
class A {
T my_value;
};
In a list, I'd like to store pointers to objects of class A. But I don't
know at forehand if these will be A<intor A<doubleor whatever (all
these flavours of A must be stored in the same list). And, lateron if I
retrieve the objects from the list I may have to know what flavour the
object was in order to call the proper routines for processing them.
My question: what does the list look like in C++? I'm lost, so if anyone
has some advice....
Thanks,
Jeroen
As a basic technique, you just overload the functions with different
possible types.Compiler will do call the appropriate one.
You can also use typeid operator to identify the type. but it will
make some overhead to your program as it enables Runtime Type
Identification. (RTTI). typeid operator returns a type_info class
which can be use for comparison, type string etc.
You can also approach boost type traits library
On Mar 6, 8:43 pm, Jeroen <no_m...@please .comwrote:
Some more explanation: it was about my matrix class with some extra
facilities. Let's say you can initialize such a matrix like (OK, a
vector in this case to keep it simple...):
{
matrix m = "1 2 3 7 8";
}
But I also want to to use variables in my string expression:
{
matrix m = "1 2 3 7 8";
matrix n;
register_user_v ar(n);
n = "m 3 4 5 8"; // concatenate vectors 'm' and [3 4 5 8]
}
Hopefully I am not wildly off base here, but it looks like you're
getting yourself in a lot of trouble trying to initialise a matrix via
a string.
Better is something like this:
Matrix<intim(3, 3); // 3x3 matrix of ints
im = 1,2,3,
4,5,6,
0,9,8;
This can be achieved by having Matrix<T>::oper ator=(T) return an
initialiser object - call it Minit<T>.
Then you define Minit<T>::opera tor,(T) to put T into the Matrix at
the next "slot", and return itself.
I have working code if you want me to post it, this technique is
nabbed from "Generative Programming" by Eisenecker and Czarnecki. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dave |
last post by:
Hello all,
Consider this function template definition:
template<typename T>
void foo(T) {}
If foo is never called, this template will never be instantiated.
Now consider this explicit instantiation of foo:
|
by: Alfonso Morra |
last post by:
if I have a class template declared as ff:
(BTW is this a partial specialization? - I think it is)
template <typename T1, myenum_1 e1=OK, my_enum_2=NONE>
class A {
public:
A();
virtual ~A() ;
|
by: Suki |
last post by:
Hi,
I'm writing a templated class, and i dont want to use the class
otherthan for some predetermined types, say, int, double etc.
This class has no meaning for typenames other than those few.
=========================
template <class T>
myClass {....} ;
|
by: mlimber |
last post by:
This is a repost (with slight modifications) from
comp.lang.c++.moderated in an effort to get some response.
I am using Loki's Factory as presented in _Modern C++ Design_ for
message passing in an embedded environment with multiple processors. I
created a policy for classes, which, I had hoped, would automatically
register the class with the appropriate factory:
// In some header file...
#include <cassert>
|
by: Leslaw Bieniasz |
last post by:
Hello,
I have the following problem:
file a.h ---------------
template <class T>
class A
{
// some stuff
| |
by: pookiebearbottom |
last post by:
Just trying to learn some things about templates. Was wondering how
boost::tupple really works, but the headers were a bit confusing to me.
I know you get do something like the following, just want to know how
it works with the overloading of get<>().
boost::tupple<int,doubletup(1,2.0);
double d=tup.get<2>(); // equal 2.0
// simple set up,
|
by: charles.lobo |
last post by:
Hi,
I have recently begun using templates in C++ and have found it to be
quite useful. However, hearing stories of code bloat and assorted
problems I decided to write a couple of small programs to check. What I
expected was that there would be minor code bloat and some speed
improvement when using templates. However...
I wrote a basic list container (using templates), and a list container
(using virtual derived classes). I also tried...
|
by: aitrob |
last post by:
Hi,
I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if I'm doing something wrong and g++ just doesn't notice, or if the people at Intel are doing something weird...
What am I trying to do? I'm trying to implement a template class that inherits from the Blitz++ array library
#include <blitz/array.h>...
|
by: David Sanders |
last post by:
Hi,
I have a class with an integer template parameter, taking values 1, 2
or 3, and a function 'calc' in that class which performs
calculations. Some calculations need only be performed if the
template parameter is 2 or 3; for efficiency, I do not wish to perform
the calculations if the template parameter is 1.
I currently do this as follows:
|
by: stdlib99 |
last post by:
Hi,
I have a simple question regarding templates and meta programming.
I am going to try and work my way through the C++ Template
Metaprogramming, a book by David Abrahams and Aleksey Gurtovoy. I’m
not doing this because I want to be a Meta Programming guru (because a
lot of that stuff looks too crazy for use in the real world).
Rather I want to learn heavyweight templates and this is the only
|
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...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |