473,763 Members | 5,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

variable type class (was: std vector use question)

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<flo at> 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(*st atic_cast<Float Vector*>(src.pt r));
else if(type = INT)
ptr = new IntVector(*stat ic_cast<IntVect or*>(src.ptr));
else if(type == STRING)
ptr = new StringVector(*s tatic_cast<Stri ngVector*>(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(co nst 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(sr c);
return *this;
}
//
// Reading data accessors
//
FloatVector& GetFloatVector( )
{ return *static_cast<Fl oatVector*>(ptr ); }
IntVector& GetIntVector()
{ return *static_cast<In tVector*>(ptr); }
StringVector& GetStringVector ()
{ return *static_cast<St ringVector*>(pt r); }

private:
//
// Delete data vector.
//
void DoCleanup() {
if(type == FLOAT)
delete static_cast<Flo atVector*>(ptr) ;
else if(type == INT)
delete static_cast<Int Vector*>(ptr);
else if(type == STRING)
delete static_cast<Str ingVector*>(ptr );
}

void *ptr;
RVarType type;
};
Jul 19 '05 #1
5 3396
"bartek d" <ba*****@SPAMZE RO.o2.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<flo at> 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
Jul 19 '05 #2

"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
Jul 19 '05 #3

"bartek d" <ba*****@SPAMZE RO.o2.pl> wrote in message
news:Xn******** *************** *@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<flo at> 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(*st atic_cast<Float Vector*>(src.pt r));
else if(type = INT)
ptr = new IntVector(*stat ic_cast<IntVect or*>(src.ptr));
else if(type == STRING)
ptr = new StringVector(*s tatic_cast<Stri ngVector*>(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(co nst 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(sr c);
return *this;
}
//
// Reading data accessors
//
FloatVector& GetFloatVector( )
{ return *static_cast<Fl oatVector*>(ptr ); }
IntVector& GetIntVector()
{ return *static_cast<In tVector*>(ptr); }
StringVector& GetStringVector ()
{ return *static_cast<St ringVector*>(pt r); }

private:
//
// Delete data vector.
//
void DoCleanup() {
if(type == FLOAT)
delete static_cast<Flo atVector*>(ptr) ;
else if(type == INT)
delete static_cast<Int Vector*>(ptr);
else if(type == STRING)
delete static_cast<Str ingVector*>(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
Jul 19 '05 #4
"John Harrison" <jo************ *@hotmail.com> wrote in
news:bg******** ****@ID-196037.news.uni-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
Jul 19 '05 #5

"bartek d" <ba*****@SPAMZE RO.o2.pl> wrote in message
news:Xn******** *************** **@153.19.0.141 ...
"John Harrison" <jo************ *@hotmail.com> wrote in
news:bg******** ****@ID-196037.news.uni-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
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
28206
by: Marcin Vorbrodt | last post by:
So I have a class Math that looks like this: Math { public: static Real PI(void); }; Real Math::PI(void) { return 4.0 * atan(1.0); }
1
1385
by: Penna Elabi | last post by:
Is class a variable? Can a variable consist of variables? For example, a vector is a class, so is this vector a variable when it includes two int variables: vector < int > exampleVector( 2, 2 );
1
3267
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data structures (ints), and more complex data structures (strings and vectors) in it, and would like to write the entire class to a data file that could then be read back and loaded. However I'm having difficulty with this -- I found out (due to an...
5
1928
by: Thomas Matthews | last post by:
Hi, I have a Display class. I would like to write a function that takes a range of objects and displays them. The range would be specified by two forward iterators: start and end (one past start). I created a base class "References" to test this concept. I want the display function to process either a vector<References> or a list<References>. However, in my compiler (Borland C++ Builder), the std::list has a different iterator...
6
1541
by: Nils | last post by:
Hi, I want to do a vector, that is the same in every template class, is it possible? Example: .... class temp<int>.... static vector A; class temp<float>... static vector A; Now I want to get acces to A without using <...>. Thank you.
7
1823
by: bartek | last post by:
Hello, I've been pondering with this for quite some time now, and finally decided to ask here for suggestions. I'm kind of confused, actually... Maybe I'm thinking too much... Brain dump follows... I need a class to represent a variable, with an associated data type and/or value. Though, I don't want it to be a variant type, and not a
4
4895
by: Serge | last post by:
Hi, I have no problem creating a static member variable with integers, etc but when I try the same with a vector then I always get linker errors that the static member variable is unknown (unresolved external symbol) Below is what an example of the code I use. Can somebody tell me what I am doing wrong ? Thanks very much in advance, Serge //myclass.h
6
6585
by: memocan | last post by:
#include <iostream> using namespace std; int x; //global variable matrix int main() { x= new float ; //initialize the size now }
28
1951
by: VK | last post by:
A while ago I wrote a "Vector data type" script using DOM interface to select.options. That was a (beautiful) mind game :-) rather than a practical thing to use. Here is another attempt open for criticism, this time dead serious. I really need an effective Vector emulator for a project (as much effective as something not implemeted in language but emulated by means of the language itself is: a
0
9563
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...
1
9937
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
9822
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
8821
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
7366
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
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
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
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2793
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.