I'm sure there's a fairly easy answer for this... but how can I define a new
type with range checking?
Example: I want to define a new type that's like a double, except that you
can only give it values from 0.0 to 100.0. I'd also like it to act like a
double as much as possible, except that an exception is thrown when it's set
to an invalid number.
Ideas?
Thanks,
Joe 10 1839
* Joe Laughlin: I'm sure there's a fairly easy answer for this... but how can I define a new type with range checking?
Example: I want to define a new type that's like a double, except that you can only give it values from 0.0 to 100.0. I'd also like it to act like a double as much as possible, except that an exception is thrown when it's set to an invalid number.
Ideas?
The difference between original C++ and C was that C++ had classes.
A class is a type.
To define a new type, define a class.
Supply the operations you want the type to have.
Get yourself a good C++ book, e.g. "Accelerated C++".
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Joe Laughlin wrote: I'm sure there's a fairly easy answer for this... but how can I define a new type with range checking?
Example: I want to define a new type that's like a double, except that you can only give it values from 0.0 to 100.0. I'd also like it to act like a double as much as possible, except that an exception is thrown when it's set to an invalid number.
Ideas?
This google groups link points to a recent discussion on comp.std.c++. http://tinyurl.com/6zyg9
Joe Laughlin posted: I'm sure there's a fairly easy answer for this... but how can I define a new type with range checking?
Example: I want to define a new type that's like a double, except that you can only give it values from 0.0 to 100.0. I'd also like it to act like a double as much as possible, except that an exception is thrown when it's set to an invalid number.
Ideas?
Thanks, Joe
Use your brain:
class RestrictiveDouble
{
public: class bad_proposal {};
private:
double data;
Set(double const proposed)
{
if (propose > 100 || proposed < 0) throw bad_proposal;
data = proposed;
}
public:
RestrictiveDouble& operator=(double const proposed)
{
Set(proposed);
}
//Copy constructor is unnecessary
//Put a constructor here
//Put an operator double here
};
I would've finished it for you, but then half way through I thought it may
have been homework for you.
-JKop
JKop wrote: Joe Laughlin posted:
I'm sure there's a fairly easy answer for this... but how can I define a new type with range checking?
Example: I want to define a new type that's like a double, except that you can only give it values from 0.0 to 100.0. I'd also like it to act like a double as much as possible, except that an exception is thrown when it's set to an invalid number.
Ideas?
Thanks, Joe
Use your brain:
class RestrictiveDouble { public: class bad_proposal {};
private:
double data;
Set(double const proposed) { if (propose > 100 || proposed < 0) throw bad_proposal;
data = proposed; }
public:
RestrictiveDouble& operator=(double const proposed) { Set(proposed); }
//Copy constructor is unnecessary
//Put a constructor here
//Put an operator double here
}; I would've finished it for you, but then half way through I thought it may have been homework for you.
-JKop
What's an "operator double"? And I'm confused why you are defining a class
bad_proposal inside of RestrictiveDouble, and then throwing it. What's an "operator double"? And I'm confused why you are defining a class bad_proposal inside of RestrictiveDouble, and then throwing it.
Sorry, I'd like to help you, but I still suspect that this is some sort of
homework question.
If you have a decent book on C++, then go to the chapter on "operator
overloading", the conversion operators will be in there with it.
As regards defining one class within another: All it means is that, instead
of the class's name being "bad_proposal", its name is
"RestrictiveDouble::bad_proposal". Also, if I were to define the
"bad_proposal" class within the private section of the "RestrictiveDouble"
class definition, then it would be inaccessible from outside of the class's
own code. (Also I wouldn't be able to throw it as the caller wouldn't be
able to play with it).
-JKop
JKop wrote: What's an "operator double"? And I'm confused why you are defining a class bad_proposal inside of RestrictiveDouble, and then throwing it. Sorry, I'd like to help you, but I still suspect that this is some sort of homework question.
Not homework.
If you have a decent book on C++, then go to the chapter on "operator overloading", the conversion operators will be in there with it.
I understand about operator overloading, just never heard of "operator
double". I've only heard of the usual operator==, operator>>, etc. As regards defining one class within another: All it means is that, instead of the class's name being "bad_proposal", its name is "RestrictiveDouble::bad_proposal". Also, if I were to define the "bad_proposal" class within the private section of the "RestrictiveDouble" class definition, then it would be inaccessible from outside of the class's own code. (Also I wouldn't be able to throw it as the caller wouldn't be able to play with it).
-JKop I understand about operator overloading, just never heard of "operator double". I've only heard of the usual operator==, operator>>, etc.
Here's the jist of it:
class Blah
{
public:
operator bool() const
{
return true;
}
};
void SomeFunc(bool const monkey)
{
}
intm main()
{
Blah blah_object;
SomeFunc(blah_object);
}
JKop wrote: I understand about operator overloading, just never heard of "operator double". I've only heard of the usual operator==, operator>>, etc.
Here's the jist of it:
class Blah { public:
operator bool() const { return true; } };
void SomeFunc(bool const monkey) {
}
intm main() { Blah blah_object;
SomeFunc(blah_object); }
So, you can use Blah anywhere you can use a bool?
Joe Laughlin posted: JKop wrote: I understand about operator overloading, just never heard of "operator double". I've only heard of the usual operator==, operator>>, etc.
Here's the jist of it:
class Blah { public:
operator bool() const { return true; } };
void SomeFunc(bool const monkey) {
}
intm main() { Blah blah_object;
SomeFunc(blah_object); }
So, you can use Blah anywhere you can use a bool?
Yep, for instance:
while (blah_object)
{
;
}
if (blah_object) ;
-JKop
In message <I6********@news.boeing.com>, Joe Laughlin
<Jo***************@boeing.com> writes JKop wrote:
[explanation of operator bool() ]
So, you can use Blah anywhere you can use a bool?
Yes, or anywhere you can use anything to which bool can be converted,
which may have effects you wouldn't expect. Implicit conversions can
produce more problems than they solve, if not used carefully.
--
Richard Herring This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Erik Max Francis |
last post by:
I've come across a limitation in unpickling certain types of complex
data structures which involve instances that override __hash__, and was
wondering if it was known (basic searches didn't seem to...
|
by: Senthilraja |
last post by:
I have the following program using templates. Someone please let me know
the syntax to be used for defining the member functions push, pop etc.
as non-inline functions.
#include <iostream>...
|
by: Matt Garman |
last post by:
I'd like to create a "custom output facility". In other words, I want
an object whose use is similar to std::cout/std::cerr, but offers more
flexibility. Instead of simply writing the parameter...
|
by: johny smith |
last post by:
If I have a simple class with say a couple of integers only is there any
need for me to provide a destructor?
thanks!
|
by: nambissan.nisha |
last post by:
I am facing this problem....
I have to define a structure at runtime as the user specifies...
The user will tell the number of fields,the actual fields...(maybe
basic or array types or...
|
by: mesut demir |
last post by:
Hi All,
When I create fields (in files) I need assign a data type like char,
varchar, money etc.
I have some questions about the data types when you create fields in a
file.
What is the...
|
by: lcaamano |
last post by:
We have a tracing decorator that automatically logs enter/exits to/from
functions and methods and it also figures out by itself the function
call arguments values and the class or module the...
|
by: Martin Z |
last post by:
Hi,
I'm trying to support a 3rd-party XML format for which there is no
schema. As such, I've been making my objects that map to their format
using the XMLSerializer... but there's one object...
|
by: lubomir dobsik |
last post by:
hi, i have seen an interesting thing:
#if sizeof((char*)0 - (char*)0) == sizeof(unsigned int)
typedef unsigned int size_t;
#elif sizeof((char*)0 - (char*)0) == sizeof(unsigned long)
typedef...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |