Re: Set of template class
Fabien wrote:
What's ZAttribute? Above, you said you have only two classes, Attribute
and SetOfAttributes.
Sorry, I made a typo, Attribute means ZAttribute to me (I just wanted
to avoid my Z prefix).
Well, you'd have to make Attribute a non-templated polymorphic class and
derive ZAttribute as a template from it. Then you have to store pointers
in your vector.
That's what I did :
class Attribute
{
virtual ~Attribute(void)
{
}
};
template <typename T>
class AttributeT : public Attribute
{
private:
T _value;
public:
inline T getValue(void) const
{
return(_value);
}
};
class SetOfAttributes
{
std::vector<Attribute> _attributes;
};
It works, but I have to cast very often because AttributeT::getValue()
doesn't exist in Attribute.
Well, there is no useful implementation in the base class, since the return
type depends on the dynamic type, so there is not much you can do about
that.
I also wanted to avoid heritage.
Well, polymorphism is the usual way to go if you want to store objects of
different types in one container.
I was wondering if there was another solution that I did not know. I
wanted to avoid using Boost, but if it's the only solution to avoid
heritage... why not.
You could have a look at boost::any and either use that or take it as an
inspiration for your own implementation.