defining new types

Discussion in 'C++' started by Joe Laughlin, Oct 21, 2004.

  1. Joe Laughlin

    Joe Laughlin Guest

    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
     
    Joe Laughlin, Oct 21, 2004
    #1
    1. Advertising

  2. * 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?
     
    Alf P. Steinbach, Oct 21, 2004
    #2
    1. Advertising

  3. 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
     
    Gianni Mariani, Oct 21, 2004
    #3
  4. Joe Laughlin

    JKop Guest

    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, Oct 21, 2004
    #4
  5. Joe Laughlin

    Joe Laughlin Guest

    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.
     
    Joe Laughlin, Oct 21, 2004
    #5
  6. Joe Laughlin

    JKop Guest


    > 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, Oct 21, 2004
    #6
  7. Joe Laughlin

    Joe Laughlin Guest

    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
     
    Joe Laughlin, Oct 21, 2004
    #7
  8. Joe Laughlin

    JKop Guest


    > 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, Oct 22, 2004
    #8
  9. Joe Laughlin

    Joe Laughlin Guest

    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, Oct 22, 2004
    #9
  10. Joe Laughlin

    JKop Guest

    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
     
    JKop, Oct 22, 2004
    #10
  11. In message <>, Joe Laughlin
    <> 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
     
    Richard Herring, Oct 25, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. johny smith
    Replies:
    8
    Views:
    422
    Peter Koch Larsen
    Jul 2, 2004
  2. Michael Yanowitz

    Defining our own types?

    Michael Yanowitz, Aug 16, 2006, in forum: Python
    Replies:
    3
    Views:
    289
    tobiah
    Aug 17, 2006
  3. flounder
    Replies:
    4
    Views:
    513
    flounder
    Apr 2, 2009
  4. Replies:
    1
    Views:
    124
    Joel VanderWerf
    Oct 25, 2004
  5. Replies:
    2
    Views:
    472
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page