By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

std vector use question

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"bartek d" <ba*****@SPAMZERO.o2.pl> wrote in message
news:Xn*************************@153.19.0.141...
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?
Jul 19 '05 #2

P: n/a

"bartek d" <ba*****@SPAMZERO.o2.pl> wrote in message
news:Xn*************************@153.19.0.141...
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
Jul 19 '05 #3

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in
news:bg************@ID-196037.news.uni-berlin.de:

(...)
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
Jul 19 '05 #4

P: n/a
"jwtroll05" <me*********@dbforums.com> wrote in
news:bg*************@news.t-online.com:

(...)
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
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.