Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > defining new types

Reply
Thread Tools

defining new types

 
 
Joe Laughlin
Guest
Posts: n/a
 
      10-21-2004
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


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-21-2004
* 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?
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      10-21-2004
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
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      10-21-2004
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
 
Reply With Quote
 
Joe Laughlin
Guest
Posts: n/a
 
      10-21-2004
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.


 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      10-21-2004

> 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
 
Reply With Quote
 
Joe Laughlin
Guest
Posts: n/a
 
      10-21-2004
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




 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      10-22-2004

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

 
Reply With Quote
 
Joe Laughlin
Guest
Posts: n/a
 
      10-22-2004
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?


 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      10-22-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining Non-Member Functions for Template Member Types flounder C++ 4 04-01-2009 11:59 PM
Defining our own types? Michael Yanowitz Python 3 08-17-2006 08:20 PM
Where are ref types that are members of value types stored? Sathyaish ASP .Net 2 05-22-2005 07:32 PM
Question on defining hier error types: Is this OK? jim@freeze.org Ruby 1 10-25-2004 10:31 PM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM



Advertisments