Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > variable type class (was: std vector use question)

Reply
Thread Tools

variable type class (was: std vector use question)

 
 
bartek d
Guest
Posts: n/a
 
      08-01-2003
Hello,

Regarding my previous question about a class which is used to store a
variable type vector. I tried to be more elaborate on the code.

I'd be grateful for your suggestions. Am I going in the wrong direction
with the implementation? I'm asking this because I don't have much
experience with C++. Thanks in advance.

The main problem I see with this class, is that the code which uses it
must first ask for its type before invoking the Get* methods.

typedef std::vector<int> IntVector;
typedef std::vector<float> FloatVector;
typedef std::vector<std::string> StringVector;

class RVar {
public:
//
// Possible var types.
//
enum RVarType {
UNDEFINED = 0, FLOAT, INT, STRING
};
//
// Default ctor.
//
RVar() : ptr(NULL), type(UNDEFINED) { }
//
// Copy ctor.
//
RVar(const RVar& src) : ptr(NULL), type(src.type) {
if(type == FLOAT)
ptr = new FloatVector(*static_cast<FloatVector*>(src.ptr));
else if(type = INT)
ptr = new IntVector(*static_cast<IntVector*>(src.ptr));
else if(type == STRING)
ptr = new StringVector(*static_cast<StringVector*>(src.ptr)) ;
}
//
// Default dtor.
//
~RVar() { if(type) DoCleanup(); }
//
// Determine the type.
//
bool TypeIs(RVarType t) {
return type == t;
}
//
// Writing data accessors
//
RVar& SetFloatVector(const FloatVector& src) {
if(type) DoCleanup();
type = FLOAT;
ptr = new FloatVector(src);
return *this;
}
RVar& SetIntVector(const IntVector& src) {
if(type) DoCleanup();
type = INT;
ptr = new IntVector(src);
return *this;
}
RVar& SetStringVector(const StringVector& src) {
if(type) DoCleanup();
type = STRING;
ptr = new StringVector(src);
return *this;
}
//
// Reading data accessors
//
FloatVector& GetFloatVector()
{ return *static_cast<FloatVector*>(ptr); }
IntVector& GetIntVector()
{ return *static_cast<IntVector*>(ptr); }
StringVector& GetStringVector()
{ return *static_cast<StringVector*>(ptr); }

private:
//
// Delete data vector.
//
void DoCleanup() {
if(type == FLOAT)
delete static_cast<FloatVector*>(ptr);
else if(type == INT)
delete static_cast<IntVector*>(ptr);
else if(type == STRING)
delete static_cast<StringVector*>(ptr);
}

void *ptr;
RVarType type;
};
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-01-2003
"bartek d" <(E-Mail Removed)2.pl> wrote...
> Regarding my previous question about a class which is used to store a
> variable type vector. I tried to be more elaborate on the code.
>
> I'd be grateful for your suggestions. Am I going in the wrong direction
> with the implementation? I'm asking this because I don't have much
> experience with C++. Thanks in advance.
>
> The main problem I see with this class, is that the code which uses it
> must first ask for its type before invoking the Get* methods.
>
> typedef std::vector<int> IntVector;
> typedef std::vector<float> FloatVector;
> typedef std::vector<std::string> StringVector;
>
> class RVar {
> public:
> //
> // Possible var types.
> //
> enum RVarType {
> UNDEFINED = 0, FLOAT, INT, STRING
> };
> [...]
> void *ptr;
> RVarType type;
> };


I'll play a devil's advocate a little. Don't get offended, it's
nothing personal...

So, what happens when somebody needs to add another type to the
set of "allowed" types? The whole class has to be changed because
it is ridden with "if-else if-else" (your garden variety "switch")
statements. Extremely inconvenient and difficult to maintain.

Can it be overcome? Yes. You need to explore templates, they
ought to be able to help. What you need to work on is minimising
of the code to be changed to add a type. When you can get to that
point, you've learned a lot about C++. [Here is the path for your
improvement study, eh?]

Bottomline is, if it works for you, of course, use it. But know
of the limitations and maintainability of the code you write. As
soon as you join a team of more than two people, the code
like your class could present a big headache.

Victor


 
Reply With Quote
 
 
 
 
Patrick Frankenberger
Guest
Posts: n/a
 
      08-01-2003

"bartek d":
> Hello,
>
> Regarding my previous question about a class which is used to store a
> variable type vector. I tried to be more elaborate on the code.
>
> I'd be grateful for your suggestions. Am I going in the wrong direction
> with the implementation? I'm asking this because I don't have much
> experience with C++. Thanks in advance.


What good does a class storing a variable type vector do? You can use
Boost::Any which can store any type, not just different vectors.
Still i think changing the design of the programm is a better idea in most
cases. Most problems can be solved in an easy way which doesn't need
"store-anything"-classes.

Patrick


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      08-01-2003

"bartek d" <(E-Mail Removed)2.pl> wrote in message
news:Xns93CA9B2AE95Ebartekdo2pl@153.19.0.141...
> Hello,
>
> Regarding my previous question about a class which is used to store a
> variable type vector. I tried to be more elaborate on the code.
>
> I'd be grateful for your suggestions. Am I going in the wrong direction
> with the implementation? I'm asking this because I don't have much
> experience with C++. Thanks in advance.


I think many people would say that you that you are going in the wrong
direction with the design. Why do you need to store unrelated types in a
vector? There is almost always a better way.

>
> The main problem I see with this class, is that the code which uses it
> must first ask for its type before invoking the Get* methods.
>
> typedef std::vector<int> IntVector;
> typedef std::vector<float> FloatVector;
> typedef std::vector<std::string> StringVector;
>
> class RVar {
> public:
> //
> // Possible var types.
> //
> enum RVarType {
> UNDEFINED = 0, FLOAT, INT, STRING
> };
> //
> // Default ctor.
> //
> RVar() : ptr(NULL), type(UNDEFINED) { }
> //
> // Copy ctor.
> //
> RVar(const RVar& src) : ptr(NULL), type(src.type) {
> if(type == FLOAT)
> ptr = new FloatVector(*static_cast<FloatVector*>(src.ptr));
> else if(type = INT)
> ptr = new IntVector(*static_cast<IntVector*>(src.ptr));
> else if(type == STRING)
> ptr = new StringVector(*static_cast<StringVector*>(src.ptr)) ;
> }
> //
> // Default dtor.
> //
> ~RVar() { if(type) DoCleanup(); }
> //
> // Determine the type.
> //
> bool TypeIs(RVarType t) {
> return type == t;
> }
> //
> // Writing data accessors
> //
> RVar& SetFloatVector(const FloatVector& src) {
> if(type) DoCleanup();
> type = FLOAT;
> ptr = new FloatVector(src);
> return *this;
> }
> RVar& SetIntVector(const IntVector& src) {
> if(type) DoCleanup();
> type = INT;
> ptr = new IntVector(src);
> return *this;
> }
> RVar& SetStringVector(const StringVector& src) {
> if(type) DoCleanup();
> type = STRING;
> ptr = new StringVector(src);
> return *this;
> }
> //
> // Reading data accessors
> //
> FloatVector& GetFloatVector()
> { return *static_cast<FloatVector*>(ptr); }
> IntVector& GetIntVector()
> { return *static_cast<IntVector*>(ptr); }
> StringVector& GetStringVector()
> { return *static_cast<StringVector*>(ptr); }
>
> private:
> //
> // Delete data vector.
> //
> void DoCleanup() {
> if(type == FLOAT)
> delete static_cast<FloatVector*>(ptr);
> else if(type == INT)
> delete static_cast<IntVector*>(ptr);
> else if(type == STRING)
> delete static_cast<StringVector*>(ptr);
> }
>
> void *ptr;
> RVarType type;
> };


You're lacking an assignment operator, you aren't using const where it would
be appropriate, other than that I don't see any code problems. A minor point
but I'd include the if (type == UNDEFINED) case in the DoCleanup function,
that way you don't have to keep saying if (type) DoCleanup();

But its real ugly, almost certainly there is a better way. Why do you need
this?

john


 
Reply With Quote
 
bartek d
Guest
Posts: n/a
 
      08-01-2003
"John Harrison" <(E-Mail Removed)> wrote in
news:bge1lb$n2ocu$(E-Mail Removed)-berlin.de:

> I think many people would say that you that you are going in the wrong
> direction with the design. Why do you need to store unrelated types in
> a vector? There is almost always a better way.


(...)

> You're lacking an assignment operator, you aren't using const where it
> would be appropriate, other than that I don't see any code problems. A
> minor point but I'd include the if (type == UNDEFINED) case in the
> DoCleanup function, that way you don't have to keep saying if (type)
> DoCleanup();


> But its real ugly, almost certainly there is a better way. Why do you
> need this?


Certainly, there's too much "C" in my way of thinking.

I need a way to keep storage of variables a'la a symbol table, also having
default values for those.

Regards,
bartek
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      08-01-2003

"bartek d" <(E-Mail Removed)2.pl> wrote in message
news:Xns93CAB818AAF40bartekdo2pl@153.19.0.141...
> "John Harrison" <(E-Mail Removed)> wrote in
> news:bge1lb$n2ocu$(E-Mail Removed)-berlin.de:
>
> > I think many people would say that you that you are going in the wrong
> > direction with the design. Why do you need to store unrelated types in
> > a vector? There is almost always a better way.

>
> (...)
>
> > You're lacking an assignment operator, you aren't using const where it
> > would be appropriate, other than that I don't see any code problems. A
> > minor point but I'd include the if (type == UNDEFINED) case in the
> > DoCleanup function, that way you don't have to keep saying if (type)
> > DoCleanup();

>
> > But its real ugly, almost certainly there is a better way. Why do you
> > need this?

>
> Certainly, there's too much "C" in my way of thinking.
>
> I need a way to keep storage of variables a'la a symbol table, also having
> default values for those.
>


OK, symbol table sounds like one of the few cases where this sort of coding
(or some other similar hack) is necessary.

john


 
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