473,796 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1605
cp********@goog lemail.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********@goog lemail.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...@com Acast.netwrote:
cppques...@goog lemail.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********@goog lemail.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_li mits<>::quiet_N aN() or std::numeric_li mits<>::infinit y().
std::numeric_li mits<>::max().
Michael
Sep 17 '07 #5
<cp********@goo glemail.comwrot e in message
news:11******** *************@r 29g2000hsg.goog legroups.com...
On Sep 17, 3:44 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>cppques...@goo glemail.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********@goog lemail.com wrote:
On Sep 17, 3:44 pm, "Victor Bazarov" <v.Abaza...@com Acast.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...@goog lemail.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_li mits<>::quiet_N aN() or std::numeric_li mits<>::infinit y().
std::numeric_li mits<>::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********@goog lemail.com wrote:
On Sep 17, 4:23 pm, Michael DOUBEZ <michael.dou... @free.frwrote:
>cppques...@goo glemail.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_l imits<>::quiet_ NaN() or
std::numeric_l imits<>::infini ty(). std::numeric_li mits<>::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

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

Similar topics

3
50363
by: Phil Powell | last post by:
<?php class SuperClass { var $mySuperClassVar; function SuperClass($myVar) { $this->mySuperClassVar = $myVar; echo "super class var = $myVar<p>"; }
2
2142
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
10836
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 equivalent to executing the default copy constructor (i.e. lines 33)? (b) If not, is the behavior for lines 36-39 well defined by the standard?
9
2069
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
3268
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
7719
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
4053
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 equivalent. Is there such a function available in C#? I can see that VB.NET has one, but I couldn't see how to get at it in C#. For example, if I have ...
8
8123
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 Activex EXE. Such a program can have a GUI which will be accessed by users in the normal way as well as exposing a COM object model. How can I do the .Net equivalent in VB.Net? According to VB.Net help, the VB.Net equivalent of an Activex EXE is...
4
6695
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 things that I like about VB.NET & there are things that I don't like. I like to do graphics like plotting math functions & fractals & stuff. VB has Pset. In VB6, it's easy to plot a pixel. Just use Pset. In VB.NET, it isn't. I have to create a...
1
6341
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
9685
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
10468
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...
0
9063
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
7559
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
6802
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
5458
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...
1
4131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2933
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.