Hello,
I have a class which is used to encapsulate a RenderMan Interface
variable.
Generally speaking, such variable may be of integral, float, string type,
or an array of those.
I thought I could implement it by using a void pointer and a dynamically
created std::vector. The vector could be accessed by typed accessor
methods which would cast it to appropriate type, or throw an exception in
case of type mismatch.
e.g.
typedef std::vector<int> IntVector;
typedef std::vector<float> FloatVector;
typedef std::vector<std::string> StringVector;
class my {
public:
my() : ptr(NULL) {}
~my() { dump(); }
my& set(const IntVector& v)
{ dump(); ptr = new IntVector(v); }
my& set(const FloatVector& v)
{ dump(); ptr = new FloatVector(v); }
my& set(const StringVector& v)
{ dump(); ptr = new StringVector(v); }
IntVector get_intv()
{ return *static_cast<IntVector*>(ptr); }
FloatVector get_floatv()
{ return *static_cast<FloatVector*>(ptr); }
StringVector get_stringv()
{ return *static_cast<StringVector*>(ptr); }
private:
void dump() { if(ptr) delete ptr; }
void *ptr;
};
My question is ... am I doing something awkward, that could get me into
some trouble at a later time? Maybe I should simply go back and write the
class from ground up and not bother using std::vector at all?
Thanks in advance for your suggestions.
regards,
bartek