std vector use question

B

bartek d

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
 
J

jwtroll05

bartek d said:
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

How about using a templated class?
 
J

John Harrison

bartek d said:
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

Its not completely clear to me how your description fits with the class you
wrote. But

1) delete ptr will not compile since ptr is declared as void*.
2) You have no means of deciding which type you are actually holding.
3) You have no sensible copying or assigning behaviour which means this
class is difficult (at best) to use in a vector (if that is your intention).
4) The copying of vectors as return values in the get_* methods is very
inefficient.

Various trivial errors as well.

john
 
B

bartek d

(...)
Its not completely clear to me how your description fits with the
class you wrote. But

1) delete ptr will not compile since ptr is declared as void*.
2) You have no means of deciding which type you are actually holding.
3) You have no sensible copying or assigning behaviour which means
this class is difficult (at best) to use in a vector (if that is your
intention). 4) The copying of vectors as return values in the get_*
methods is very inefficient.

Ok my bad - I didn't mark it as a "sketch".
My point wasn't about the details, but rather about the general idea of
using a dynamically created std::vector. Is it a way?

I thought people in this newsgroup are more tolerant than a C++ compiler.
;)

regards,
bartek
 
B

bartek d

(...)
How about using a templated class?

A templated class would be bound to a single type.
While I'd like it to contain one of several types, which can change at
runtime.

regards,
bartek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top