Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std vector use question

Reply
Thread Tools

std vector use question

 
 
bartek d
Guest
Posts: n/a
 
      07-31-2003
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
 
Reply With Quote
 
 
 
 
jwtroll05
Guest
Posts: n/a
 
      07-31-2003
"bartek d" <> wrote in message
news:Xns93C9F13713364bartekdo2pl@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?


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-31-2003

"bartek d" <> wrote in message
news:Xns93C9F13713364bartekdo2pl@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


 
Reply With Quote
 
bartek d
Guest
Posts: n/a
 
      07-31-2003
"John Harrison" <> wrote in
news:bgc5ja$n8au8$:

(...)

> 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
 
Reply With Quote
 
bartek d
Guest
Posts: n/a
 
      08-01-2003
"jwtroll05" <> wrote in
news:bgc3m7$vf7$05$:

(...)

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory footprint of std::vector<std::vector<T> > Rune Allnor C++ 4 12-11-2008 05:44 PM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Removing a vector element using std::swap and std::vector::resize. Jason Heyes C++ 8 01-15-2006 10:40 PM
Convert from std::vector<double> to std::vector<int> Anonymous C++ 20 03-30-2005 03:20 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57