473,761 Members | 8,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<flo at> 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<In tVector*>(ptr); }

FloatVector get_floatv()
{ return *static_cast<Fl oatVector*>(ptr ); }

StringVector get_stringv()
{ return *static_cast<St ringVector*>(pt r); }

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 6574
"bartek d" <ba*****@SPAMZE RO.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<flo at> 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<In tVector*>(ptr); }

FloatVector get_floatv()
{ return *static_cast<Fl oatVector*>(ptr ); }

StringVector get_stringv()
{ return *static_cast<St ringVector*>(pt r); }

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*****@SPAMZE RO.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<flo at> 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<In tVector*>(ptr); }

FloatVector get_floatv()
{ return *static_cast<Fl oatVector*>(ptr ); }

StringVector get_stringv()
{ return *static_cast<St ringVector*>(pt r); }

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*********@db forums.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
11433
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 values to a 2D vector, of which every vector has the same length. I tried the following but I get a "System.NullReferenceException" error when I ran it.
10
7079
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 clear(), in DOT.NET the capacity() is reduced to 0.
12
3160
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 necessarily a C language question. I'm trying to break up a vector into an arbitrary number of subvectors, equal (or as near to equal) in size as possible. My problem occurs when the vector is not evenly divisible by the number of subvectors...
24
2957
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 vector<Corres>{ public: void addCorres(Corres& c); //it do little more than push_back function. }
2
3322
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 fault which I resolved by doing vector<vector<int example; example.push_back(vector<int>());
9
3759
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" contains built-in types (e.g. int), then "clear()" doesn't remove anything at all. Why does "clear()" have this behaviour? Also, when I copy one vector "v1" from another vector "v2", with "v1" longer than "v2" (e.g. "v1" has 2 elements and "v2" has...
7
3895
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 allocate multidimentional arrays like so: int *v_base = (int *) malloc(1000000*sizeof(int)); int **v = (int **) malloc(1000*sizeof(int *));
6
11616
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 push_back it, or could i pushback the two ints directly somehow? Thanks for all.
13
1925
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 this? I wrote the following program to test this out. #include <fstream> #include <iostream> #include <string>
6
7383
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", new vector<string>)); MapVector.insert(make_pair("string2", new vector<string>)); MapVector.insert(make_pair("string3", new vector<string>));
0
9522
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10111
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.