473,321 Members | 1,667 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,321 software developers and data experts.

defining new types

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
Jul 22 '05 #1
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?
Jul 22 '05 #2
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
Jul 22 '05 #3
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
Jul 22 '05 #4
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.
Jul 22 '05 #5
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
Jul 22 '05 #6
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


Jul 22 '05 #7
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);
}

Jul 22 '05 #8
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?
Jul 22 '05 #9
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
Jul 22 '05 #10
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
Jul 22 '05 #11

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

Similar topics

1
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...
7
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>...
12
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...
8
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!
10
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...
11
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...
2
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...
2
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...
8
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: 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...
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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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...
0
isladogs
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...

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.