Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #10712
| From | softwareEngineer <ovNOSPAM@NOSPAMlibero.it> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: template with STL container |
| Date | 2011-10-07 10:42 +0200 |
| Organization | C.U. srl News Server |
| Message-ID | <4E8EBB59.1000507@NOSPAMlibero.it> (permalink) |
| References | <j6f3eg$e4r$1@tdi.cu.mi.it> <j6f3kh$hd0$1@dont-email.me> <4E8B1520.80102@NOSPAMlibero.it> <pan.2011.10.05.08.52.07.605000@nowhere.com> <j6i9v5$1tje$1@adenine.netfront.net> |
Il 05/10/2011 21:08, Paul <pchrist ha scritto:
> "Nobody"<nobody@nowhere.com> wrote in message
> news:pan.2011.10.05.08.52.07.605000@nowhere.com...
>> On Tue, 04 Oct 2011 16:16:00 +0200, softwareEngineer wrote:
>>
>>> Yes, but I need a vector of generic value (float, int, ecc.)
>> In which case, the values need a common base class, e.g.:
>>
>> class MY_Value {
>> ...
>> };
>>
>> template<typename T>
>> class MY_Value_T : public MY_Value {
>> ...
>> };
>>
>> std::vector<MY_Value*> v;
>>
>> MyValue_T<int> iV1 = 1;
>> MyValue_T<float> iV2 = 2.1;
>> v[0] = static_cast<MY_Value*>(&iV1);
>> v[1] = static_cast<MY_Value*>(&iV2);
>>
>> Casting the vector elements back to their correct type is a bit more
>> involved, as you need to know what their correct type is, either by
>> using RTTI or by explicitly adding a tag field to the base class.
>>
> You can do this kind of thing if you have a limited set of different types:
>
> template<class T1=float, class T2=int, class T3=std::string>
> class Base{
> public:
> virtual operator T1(){return 0;}
> virtual operator T2(){return 0;}
> virtual operator T3(){return 0;}
> };
>
> template<class T, class T1=float, class T2=int, class T3=std::string>
> class Derived:public Base<T1,T2,T3>{
> T itsData;
> public:
> Derived(T x):itsData(x){};
> virtual operator T(){return itsData;}
> };
>
> Derived<float> d1(-4.0/3.0);
> Derived<std::string> d2("Hello");
>
> std::vector< Base<>*> v1;
> v1.push_back(&d1);
> float f1 = *v1[0];
> v1[0] =&d2;
> std::string s1= *v1[0];
>
> Its more difficult if you want unlimited amount of different types.
>
>
>
> --- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
Yes,
it's exactly what I meant ...
bye.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread
template with STL container softwareEngineer <ovNOSPAM@NOSPAMlibero.it> - 2011-10-04 16:00 +0200
Re: template with STL container Victor Bazarov <v.bazarov@comcast.invalid> - 2011-10-04 10:01 -0400
Re: template with STL container softwareEngineer <ovNOSPAM@NOSPAMlibero.it> - 2011-10-04 16:16 +0200
Re: template with STL container Victor Bazarov <v.bazarov@comcast.invalid> - 2011-10-04 10:44 -0400
Re: template with STL container Noah Roberts <roberts.noah@gmail.com> - 2011-10-04 07:48 -0700
Re: template with STL container Juha Nieminen <nospam@thanks.invalid> - 2011-10-04 19:14 +0000
Re: template with STL container Nobody <nobody@nowhere.com> - 2011-10-05 09:51 +0100
Re: template with STL container Victor Bazarov <v.bazarov@comcast.invalid> - 2011-10-05 08:05 -0400
Re: template with STL container Juha Nieminen <nospam@thanks.invalid> - 2011-10-05 15:17 +0000
Re: template with STL container Gert-Jan de Vos <gert-jan.de.vos@onsneteindhoven.nl> - 2011-10-05 05:43 -0700
Re: template with STL container "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-10-05 20:08 +0100
Re: template with STL container softwareEngineer <ovNOSPAM@NOSPAMlibero.it> - 2011-10-07 10:42 +0200
csiph-web