472,805 Members | 915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

std vector use question

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
4 6511
"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

"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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jessica | last post by:
Hi, I do not have a lot of experience with STL and I hope some of you might be able to help me on this seemingly elementary question. I have a vector of doubles (v1). I am trying to copy the...
10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
12
by: No Such Luck | last post by:
Hi All: I'm not sure if this is the right place to ask this question, but I couldn't find a more appropriate group. This is more of a theory question regarding an algorithm implemented in C, not...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
2
by: danielhdez14142 | last post by:
Some time ago, I had a segment of code like vector<vector<int example; f(example); and inside f, I defined vector<int>'s and used push_back to get them inside example. I got a segmentation...
9
by: Jess | last post by:
Hello, I tried to clear a vector "v" using "v.clear()". If "v" contains those objects that are non-built-in (e.g. string), then "clear()" can indeed remove all contents. However, if "v"...
7
by: nw | last post by:
Hi, We've been having a discussion at work and I'm wondering if anyone here would care to offer an opinion or alternative solution. Aparently in the C programming HPC community it is common to...
6
by: jmsanchezdiaz | last post by:
CPP question: if i had a struct like "struct str { int a; int b };" and a vector "std::vector < str test;" and wanted to push_back a struct, would i have to define the struct, fill it, and then...
13
by: prasadmpatil | last post by:
I am new STL programming. I have a query regarding vectors. If I am iterating over a vector using a iterator, but do some operations that modify the size of the vector. Will the iterator recognize...
6
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1",...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.