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 4 6549
"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?
"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
"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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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"...
|
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...
|
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...
|
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...
|
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",...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |