defining new types

J

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?

Thanks,
Joe
 
A

Alf P. Steinbach

* 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++".
 
G

Gianni Mariani

Joe said:
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
 
J

JKop

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
 
J

Joe Laughlin

JKop said:
Joe Laughlin posted:



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.
 
J

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.

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
 
J

Joe Laughlin

JKop said:
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.
 
J

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);
}
 
J

Joe Laughlin

JKop said:
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?
 
R

Richard Herring

Joe Laughlin said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top