473,320 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

equivalent class to POD

I got an app where a bunch of values are set before the calculation is
fired up.

I need an elegant way to check if all variables have been set.
So for example for double I am thinking
about a

class Double
{
private;
double val;
bool init;

public:
Double(): init(false) {}
Double( double v): val( v), init( true) {}

double Get() { if( !init) throw "Error"; return val;}
};

For calculations I might have to define many operators.

Apart from that: Into which trouble am I running?
As I am probably not the first to think about this basic issue,
is there already an implemtation for the basic POD available?

Thanks,
Marc

Sep 17 '07 #1
10 1568
cp********@googlemail.com wrote:
I got an app where a bunch of values are set before the calculation is
fired up.

I need an elegant way to check if all variables have been set.
What does it mean for a variable to "be set"?
So for example for double I am thinking
about a

class Double
{
private;
double val;
bool init;

public:
Double(): init(false) {}
Why do you need this?
Double( double v): val( v), init( true) {}
If you only implement this, there is no need for the 'init' flag.
>
double Get() { if( !init) throw "Error"; return val;}
};

For calculations I might have to define many operators.
I am not sure this is relevant at all.
Apart from that: Into which trouble am I running?
I am not certain what the reason would be to provide a way to
*not* initialise an object which will throw an exception when
accessed *instead of* simply requiring for an object to always
be in a valid state by implementing only the constructor with
the argument.
As I am probably not the first to think about this basic issue,
is there already an implemtation for the basic POD available?
What for? PODs are a hack, necessary for C compatibility. Try
to see beyond them. Initialise all objects with proper values.
Then you will have no such problem.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 17 '07 #2
On 2007-09-17 14:07, cp********@googlemail.com wrote:
I got an app where a bunch of values are set before the calculation is
fired up.

I need an elegant way to check if all variables have been set.
Write your code carefully and review it afterwards.
So for example for double I am thinking about a

class Double
{
private;
double val;
bool init;

public:
Double(): init(false) {}
Double( double v): val( v), init( true) {}

double Get() { if( !init) throw "Error"; return val;}
};

For calculations I might have to define many operators.

Apart from that: Into which trouble am I running?
You are probably not the first person to think of the idea to wrap built
in types in classes to provide additional error checking or function-
ality. If done correctly you should have little or no extra overhead
(except from the actual checks). However to do it right is quite a lot
of work, and while using templates will mitigate somewhat it is still
not a small task. If I were you I think twice about doing this unless
you plan to use it in a lot of places so that the extra development time
is small compared to the total development time.
As I am probably not the first to think about this basic issue,
is there already an implemtation for the basic POD available?
The best solution is probably to carefully review all code-paths to make
sure that you never operate on unset data. Then add asserts to make sure
of it.

--
Erik Wikström
Sep 17 '07 #3
On Sep 17, 3:44 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
cppques...@googlemail.com wrote:
I got an app where a bunch of values are set before the calculation is
fired up.
I need an elegant way to check if all variables have been set.

What does it mean for a variable to "be set"?
to be initialized. Set to some value.

So for example for double I am thinking
about a
class Double
{
private;
double val;
bool init;
public:
Double(): init(false) {}

Why do you need this?
Double( double v): val( v), init( true) {}

If you only implement this, there is no need for the 'init' flag.
double Get() { if( !init) throw "Error"; return val;}
};
For calculations I might have to define many operators.

I am not sure this is relevant at all.
Double a(3.0), b(4.9);
Double c = a + b;
operator+ and operator=
would need to be defined.

Apart from that: Into which trouble am I running?

I am not certain what the reason would be to provide a way to
*not* initialise an object which will throw an exception when
accessed *instead of* simply requiring for an object to always
be in a valid state by implementing only the constructor with
the argument.
It is a class with lots of parameters (memebers of type double).
These values need to be set by some setting member functions
(like e.g.:
void SetParam1( double value);
).
When the calculation is requested, I need a way to check if all
relevant parameters have been set.

As some of the parameters might take any value, just setting a default
value is not
sufficient.

In the setting member functions I could of course do some checking,
but for every
newly introduced parameter (extending the program) these functions
would need
to be adapted then (and result in a bool flag per parameter).

As I am probably not the first to think about this basic issue,
is there already an implemtation for the basic POD available?

What for? PODs are a hack, necessary for C compatibility. Try
to see beyond them. Initialise all objects with proper values.
Then you will have no such problem.
The proper values are not known at construction time.

Another advantage of custom double replacement (class Double) would be
extensions like range checking. My example was just as simple as
possible.


Sep 17 '07 #4
cp********@googlemail.com a écrit :
I got an app where a bunch of values are set before the calculation is
fired up.

I need an elegant way to check if all variables have been set.
So for example for double I am thinking

Apart from that: Into which trouble am I running?
As I am probably not the first to think about this basic issue,
is there already an implemtation for the basic POD available?
An old method is to use dead value if available (nan for float, max for
int) at initialization and then assert them:
See
std::numeric_limits<>::quiet_NaN() or std::numeric_limits<>::infinity().
std::numeric_limits<>::max().
Michael
Sep 17 '07 #5
<cp********@googlemail.comwrote in message
news:11*********************@r29g2000hsg.googlegro ups.com...
On Sep 17, 3:44 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>cppques...@googlemail.com wrote:
I got an app where a bunch of values are set before the calculation is
fired up.
I need an elegant way to check if all variables have been set.

What does it mean for a variable to "be set"?

to be initialized. Set to some value.

So for example for double I am thinking
about a
class Double
{
private;
double val;
bool init;
public:
Double(): init(false) {}

Why do you need this?
Double( double v): val( v), init( true) {}

If you only implement this, there is no need for the 'init' flag.
double Get() { if( !init) throw "Error"; return val;}
};
For calculations I might have to define many operators.

I am not sure this is relevant at all.

Double a(3.0), b(4.9);
Double c = a + b;
operator+ and operator=
would need to be defined.

Apart from that: Into which trouble am I running?

I am not certain what the reason would be to provide a way to
*not* initialise an object which will throw an exception when
accessed *instead of* simply requiring for an object to always
be in a valid state by implementing only the constructor with
the argument.

It is a class with lots of parameters (memebers of type double).
These values need to be set by some setting member functions
(like e.g.:
void SetParam1( double value);
).
When the calculation is requested, I need a way to check if all
relevant parameters have been set.

As some of the parameters might take any value, just setting a default
value is not
sufficient.

In the setting member functions I could of course do some checking,
but for every
newly introduced parameter (extending the program) these functions
would need
to be adapted then (and result in a bool flag per parameter).
And if you have a bool flag per parameter, you have to remember to set the
bool flag for the constructor, assignment operator and copy constructor.
And since you have to remember to set the bool flag, you might as well
remember to set the variable itself. In other words, this gains you nothing
but a maintainance headache.
As I am probably not the first to think about this basic issue,
is there already an implemtation for the basic POD available?

What for? PODs are a hack, necessary for C compatibility. Try
to see beyond them. Initialise all objects with proper values.
Then you will have no such problem.

The proper values are not known at construction time.

Another advantage of custom double replacement (class Double) would be
extensions like range checking. My example was just as simple as
possible.


Sep 17 '07 #6
In the setting member functions I could of course do some checking,
but for every
newly introduced parameter (extending the program) these functions
would need
to be adapted then (and result in a bool flag per parameter).

And if you have a bool flag per parameter, you have to remember to set the
bool flag for the constructor, assignment operator and copy constructor.
And since you have to remember to set the bool flag, you might as well
remember to set the variable itself. In other words, this gains you nothing
but a maintainance headache.
You mention three places! Three places instead of dozens of
parameters.
Which are set and later maybe reset.
This gains me a lot I guess.

It is even feasible that all paramteters insert pointers to themselfs
into some container,
making invalidation or serialization easy.

So still: Where is the drawback?
If there is one.
If not: Why is not such a thing readily available a boost.org or
similar?

Sep 17 '07 #7
cp********@googlemail.com wrote:
On Sep 17, 3:44 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>[..]
I am not certain what the reason would be to provide a way to
*not* initialise an object which will throw an exception when
accessed *instead of* simply requiring for an object to always
be in a valid state by implementing only the constructor with
the argument.

It is a class with lots of parameters (memebers of type double).
These values need to be set by some setting member functions
(like e.g.:
void SetParam1( double value);
).
When the calculation is requested, I need a way to check if all
relevant parameters have been set.
OK, so you're saying that for some operations only some members are
required, and for other operations other members are required to have
valid values, right? IOW, an object is allowed to have "part of it"
invalid if the operation does not require it.

That seems to require a flag for every operation (IOW, you would need
to either set all flags ahead of time by combining the validity of
the members in some manner, or set the validity flags and then every
operation will do the hard-coded check by combining the flags it knows
about, again in some manner). Too much work, and for what gain?
As some of the parameters might take any value, just setting a default
value is not
sufficient.

In the setting member functions I could of course do some checking,
but for every
newly introduced parameter (extending the program) these functions
would need
to be adapted then (and result in a bool flag per parameter).

>>As I am probably not the first to think about this basic issue,
is there already an implemtation for the basic POD available?

What for? PODs are a hack, necessary for C compatibility. Try
to see beyond them. Initialise all objects with proper values.
Then you will have no such problem.

The proper values are not known at construction time.
Then you might want to reconsider your model.
Another advantage of custom double replacement (class Double) would be
extensions like range checking.
That's not an extension, that's a limitation.
My example was just as simple as
possible.
It was most likely too simple.

I am guessing that even I can come up with a made-up example where not
all members would be "set" at the construction of an object and then
if an operation were to be performed, a check would be done and a green
light would be given if all needed parameters are set, and an exception
would be thrown otherwise.

I can even think of an example from the Standard library: a vector that
has N elements, N < M, can have new values inserted into, but cannot be
asked to supply any element with the index N..M-1, but this is not the
same, unfortunately. All member values _are_ set, they just don't meet
certain conditions. Same with, for example, a standard stream that was
read past the last element - the stream is in EOF condition - you can't
read from it, but you can close it. Again, all elements are set, but
not all operations can be performed (or lead to meaningful result).

Now, I imagine a car on the assembly line is closer. You can't really
sit inside [comfortably] until the seats are installed. You can't turn
it on until the engine is there. You can't move it independently until
the wheels have been attached, etc. But those contributing elements of
a car should then be probably modeled by pointers since nobody really
expects the car to be driven, or sat in, while the car is still on the
assembly line, and the only functionality it is expected to provide is
to be further assembled (completed). Individual tests could be still
performed (like turing on the lights to check connectivity). And the
system that performs those tests would of course (a) check if the system
to be test is present and reports (alleges) to be exercisable and (b)
that all exceptions are caught in the same scope where the test is
performed. IOW, the availability of the members that participate in
a certain operation is exposed to the object manager (and not limited
to the object itself so it could throw the exception when the operation
is attempted).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 17 '07 #8
On Sep 17, 4:23 pm, Michael DOUBEZ <michael.dou...@free.frwrote:
cppques...@googlemail.com a écrit :
I got an app where a bunch of values are set before the calculation is
fired up.
I need an elegant way to check if all variables have been set.
So for example for double I am thinking
Apart from that: Into which trouble am I running?
As I am probably not the first to think about this basic issue,
is there already an implemtation for the basic POD available?

An old method is to use dead value if available (nan for float, max for
int) at initialization and then assert them:
See
std::numeric_limits<>::quiet_NaN() or std::numeric_limits<>::infinity().
std::numeric_limits<>::max().

Michael
I thought about this as well. It might be feasible but at least for
ints
it is rather unelegant, if all values might be valid (lets say at
least all
integral parameters together might very well cover the full range).
And for double or float NaN might be the result of some calculation as
well
(or can this never be quiet_NaN(), i.e. is quiet_NaN() especially made
for such a case ?)

However, I might fall back to this solution if it turns out that the
flagged
values (class Double) do have some drawbacks.
Sep 17 '07 #9
cp********@googlemail.com wrote:
On Sep 17, 4:23 pm, Michael DOUBEZ <michael.dou...@free.frwrote:
>cppques...@googlemail.com a écrit :
>>I got an app where a bunch of values are set before the calculation
is fired up.
>>I need an elegant way to check if all variables have been set.
So for example for double I am thinking
>>Apart from that: Into which trouble am I running?
As I am probably not the first to think about this basic issue,
is there already an implemtation for the basic POD available?

An old method is to use dead value if available (nan for float, max
for int) at initialization and then assert them:
See
std::numeric_limits<>::quiet_NaN() or
std::numeric_limits<>::infinity(). std::numeric_limits<>::max().

Michael

I thought about this as well. It might be feasible but at least for
ints
it is rather unelegant, if all values might be valid (lets say at
least all
integral parameters together might very well cover the full range).
And for double or float NaN might be the result of some calculation as
well
(or can this never be quiet_NaN(), i.e. is quiet_NaN() especially made
for such a case ?)
Should it matter whether the value comes from some prior calculation or
from being "unset" -- it still cannot be used in the further operation,
can it? That would make the checking relatively simple, but the error
message should be different; not "value has not been set" but rather
"current value is not appropriate for the requested operation" (like the
domain error, e.g. negative values for calculating a logarithm of).
However, I might fall back to this solution if it turns out that the
flagged
values (class Double) do have some drawbacks.
At this point you should either make Double aware of all possible
operations so that each Double could say whether it is appropriate or
not. That is rather backwards if you intended to make Double simple
and efficient. For efficiency those flags need to be cached, and the
Double objects will be bloated with cached validity flags. Or you'll
need to sacrifice the granularity and freedom and define several
validity flags (or range flags) to be shared between some operations
(which is probably an acceptable compromise). In any way, you have
more designing to do, methinks.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 17 '07 #10
cp********@googlemail.com writes:
I got an app where a bunch of values are set before the calculation is
fired up.

I need an elegant way to check if all variables have been set.
Just some ideas to think about: When I need to do this, I often use a
pointer to that object, which can be NULL if it is unset, or a default
flag value that doesn't otherwise make sense in the application, for
example INT_MAX for an application that should use fairly small
integers, or nan() for an application that should use a float/double
but shouldn't accept "not a number" as valid. C will sometimes extend
the type of a return value to make room for extra information, for
example get() returns an int instead of an unsigned char, so that -1
can mean "end-of-file"; I can't think of similar examples in standard
C++, but it should still be a usable technique.

Another thought: Java and C# call standard data types with object
semantics (including being settable to NULL) "boxed types". That
might be a useful keyword to search for.

As far as I can see, the technique you propose should work, but sounds
tedious.

Hope this helps,

-----Scott.
Sep 17 '07 #11

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

Similar topics

3
by: Phil Powell | last post by:
<?php class SuperClass { var $mySuperClassVar; function SuperClass($myVar) { $this->mySuperClassVar = $myVar; echo "super class var = $myVar<p>"; }
2
by: Job Lot | last post by:
I have an enumeration as follows Public Enum Delimiters Tab Semicolon Comma Space End Enum How can I return character equivalent of the elements in the enumeration?
16
by: jonathan cano | last post by:
QUESTION: In practice, lines 36 and 37 below are usually equivalent to the default copy constructor (.e.g line 33). My questions are: (a) Does ISO 14882 guarantee that lines 36 and 37 are...
9
by: Krishnan | last post by:
Hi, Just curious to know if there's a keyword equivalent to "Myclass" in VB.Net in C# TIA Krishnan
8
by: FDude | last post by:
In otherwords, how do I force a method in my class to call the method implementations in the SAME class and not use any potentially overriden methods in derived classes?
2
by: yaron | last post by:
Hi, for porting java to c# i need to know: What is the C#.NET equivalent for java.net.SocketAddress and java.nio.ByteBuffer ? Thanks.
9
by: Alan Silver | last post by:
Hello, I'm converting some old VB6 code to use with ASP.NET and have come unstuck with the Asc() function. This was used in the old VB6 code to convert a character to its ASCII numeric...
8
by: Nikkita | last post by:
Software such as Excel or Word exposes a COM object model which allows them to be automated by any language that can call COM objects. As I understand it, I can do the same in VB6 by writing an...
4
by: pcnerd | last post by:
I've been playing with "classic" VB since version 3. I have VB6 Learning Edition. Recently, I wanted to try VB.NET. I got a beginner's book with a CD with the software & installed it. There are...
1
by: trialproduct2004 | last post by:
Hi all, can any one tell me use of shadow keyword in vb.net and its equivalent in c# with e.g. Please help me. thanks in advance.
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.