std vector use question

Discussion in 'C++' started by bartek d, Jul 31, 2003.

  1. bartek d

    bartek d Guest

    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
    bartek d, Jul 31, 2003
    #1
    1. Advertising

  2. bartek d

    jwtroll05 Guest

    "bartek d" <bartekd@SPAMZERo_O2.pl> 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?
    jwtroll05, Jul 31, 2003
    #2
    1. Advertising

  3. "bartek d" <bartekd@SPAMZERo_O2.pl> 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
    John Harrison, Jul 31, 2003
    #3
  4. bartek d

    bartek d Guest

    "John Harrison" <> wrote in
    news:bgc5ja$n8au8$-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
    bartek d, Aug 1, 2003
    #4
  5. bartek d

    bartek d Guest

    "jwtroll05" <> wrote in
    news:bgc3m7$vf7$05$-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
    bartek d, Aug 1, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,265
    Ivan Vecerina
    Mar 17, 2005
  2. Anonymous
    Replies:
    20
    Views:
    4,272
    Pete Becker
    Mar 30, 2005
  3. Jason Heyes
    Replies:
    8
    Views:
    708
    Andrew Koenig
    Jan 15, 2006
  4. Replies:
    8
    Views:
    1,890
    Csaba
    Feb 18, 2006
  5. Rune Allnor
    Replies:
    4
    Views:
    924
    Rune Allnor
    Dec 11, 2008
Loading...

Share This Page