Home > Archive > VC STL > March 2006 > Use template class as _Ty of vector?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Use template class as _Ty of vector?
|
|
|
| I have the following definition:
template <class T> item {......};
vector <item> itemList;
The compiler gave error. However, the following definition works:
vector <item<int> > itemList;
vector <item<float> > itemList;
But the items of the vector can be item<int> or item<float> or
item<string>....
Any elegant solution?
| |
| Igor Tandetnik 2006-03-08, 7:03 pm |
| nick <nick@discussions.microsoft.com> wrote:
> I have the following definition:
>
> template <class T> item {......};
> vector <item> itemList;
Which item is it a vector of? item<int> ? item<long> ?
item<SomethingElse> ? These are three distinct classes.
> The compiler gave error. However, the following definition works:
> vector <item<int> > itemList;
> vector <item<float> > itemList;
>
> But the items of the vector can be item<int> or item<float> or
> item<string>....
Have an abstact base class defining what all these items have in common.
Store pointers to this base class in the vector, perhaps with the help
of boost::shared_ptr or similar.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
| |
|
| I have the following code:
class Vistor;
struct ParamBase
{
virtual ~ParamBase(){}
std::string fieldName;
long dataType;
virtual void accept(Vistor& visitor) = 0;
};
template <class T>
struct Param: ParamBase
{
T value;
void accept(Vistor& visitor)
{
visitor.visit(*this);
}
};
class Vistor
{
public:
virtual ~Vistor() {}
virtual void visit(Param<std::string>& param) = 0;
virtual void visit(Param<int>& param) = 0;
//other types you want to support
};
vector <ParamBase> input_params;
However, the compiler complained that
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\vector(506):
error C2259: 'ParamBase' : cannot instantiate abstract class
"Igor Tandetnik" wrote:
> nick <nick@discussions.microsoft.com> wrote:
>
> Which item is it a vector of? item<int> ? item<long> ?
> item<SomethingElse> ? These are three distinct classes.
>
>
> Have an abstact base class defining what all these items have in common.
> Store pointers to this base class in the vector, perhaps with the help
> of boost::shared_ptr or similar.
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
>
>
>
| |
| Igor Tandetnik 2006-03-09, 7:02 pm |
| nick <nick@discussions.microsoft.com> wrote:
> struct ParamBase
> {
> virtual ~ParamBase(){}
> std::string fieldName;
> long dataType;
> virtual void accept(Vistor& visitor) = 0;
> };
>
> vector <ParamBase> input_params;
>
> However, the compiler complained that
> c:\Program Files\Microsoft Visual Studio .NET
> 2003\Vc7\include\vector(506): error C2259: 'ParamBase' : cannot
> instantiate abstract class
Which part of "store _pointers_ to this base class" do you have
difficulty understanding?
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
| |
| Arnaud Debaene 2006-03-10, 7:01 pm |
| nick wrote:
> I have the following code:
>
> class Vistor;
>
> struct ParamBase
> {
> virtual ~ParamBase(){}
> std::string fieldName;
> long dataType;
> virtual void accept(Vistor& visitor) = 0;
> };
>
> template <class T>
> struct Param: ParamBase
> {
> T value;
> void accept(Vistor& visitor)
> {
> visitor.visit(*this);
> }
> };
>
> class Vistor
> {
> public:
> virtual ~Vistor() {}
> virtual void visit(Param<std::string>& param) = 0;
> virtual void visit(Param<int>& param) = 0;
> //other types you want to support
> };
>
> vector <ParamBase> input_params;
Use vector<ParamBase*>, or much better vector<boost::shared_ptr<ParamBase>
>.
One reason wyh it is impossible to directly store derived classes in a
vector is that all elements of the vector must all have the same size (just
as a plain old C array).
Arnaud
MVP - VC
|
|
|
|
|