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

Vector and unspecialized class template

Hey,

I have a templated class (code follows) that I wish to put
into a vector, however I wish to put multiple of the class
into the vector and all might vary in what type they
represent.

Is there a way for me to to this without using multiple
vectors for each type of the class ?

(PS: I'm new to programming)
Code Snippet:

#include <iostream>
#include <vector>

template <typename Tclass keeper {
private:
T& value;
public:
keeper(T& in_value) : value(in_value) {
}

~keeper () {
}
};

int main () {

float my_float_value = 0.1f;
int my_int_value = 129;

keeper<floatmy_float(my_float_value);
keeper<intmy_int(my_int_value);

/* this one needs to be changed */
std::vector< keeper my_keepers;

return(0);
}
Oct 27 '07 #1
2 1690
Ek.H wrote:
Hey,
Hey yourself.
I have a templated class (code follows) that I wish to put
into a vector, however I wish to put multiple of the class
into the vector and all might vary in what type they
represent.
A standard vector is a template whose first argument is a class.
As soon as you try instantiating a vector, you need to provide
a *single* *complete* class. There can be no "multiple". Such
is the rule.
Is there a way for me to to this without using multiple
vectors for each type of the class ?
No.

However, if you want to keep different objects collected in
some kind of container, you should research "heterogeneous
container" on the web.
(PS: I'm new to programming)
Code Snippet:

#include <iostream>
#include <vector>

template <typename Tclass keeper {
private:
T& value;
public:
keeper(T& in_value) : value(in_value) {
}

~keeper () {
}
};

int main () {

float my_float_value = 0.1f;
int my_int_value = 129;

keeper<floatmy_float(my_float_value);
keeper<intmy_int(my_int_value);

/* this one needs to be changed */
std::vector< keeper my_keepers;

return(0);
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 27 '07 #2
Ek.H wrote:
Hey,

I have a templated class (code follows) that I wish to put
into a vector, however I wish to put multiple of the class
into the vector and all might vary in what type they
represent.
... What Victor said.

and

The way this is normally done is by storing pointers to a base class of
keeper<Tin the vector - or - by using an "any" class - like boost::any
or Austria C++ Any.
Oct 27 '07 #3

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

Similar topics

1
by: Alan Benn | last post by:
(VC6) When I use the STL <vector> template as follows: #include <vector> .... vector<CString> m_nameList; // Names of the chips I get these compiler warnings : C:\Program...
7
by: Frank Bormann | last post by:
Hi guys, I'm a C++ beginner. I was wondering if there is a generalized vector base class, through which I could pass a pointer to any kind of vector to a function, regardless of what type of...
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter;...
12
by: mast2as | last post by:
Hi everyone I am working on some code that uses colors. Until recently this code used colors represented a tree floats (RGB format) but recently changed so colors are now defined as spectrum....
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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:
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...

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.