By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,799 Members | 1,362 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,799 IT Pros & Developers. It's quick & easy.

defining new types

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
* 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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.