Inheriting scalars

Discussion in 'C++' started by Ray Gardener, Jun 9, 2004.

  1. Ray Gardener

    Ray Gardener Guest

    C++ doesn't allow:

    class counter : public int {};


    Was there any special reason why base class ids can only be
    struct/class ids? This limitation appears to violate the
    concept of uniform type conceptualization; i.e. classes
    can never have or extend an is-a relationship to a scalar.


    Ray Gardener
    Daylon Graphics Ltd.
    http://www.daylongraphics.com
    "Heightfield modeling perfected"
     
    Ray Gardener, Jun 9, 2004
    #1
    1. Advertising

  2. Ray Gardener

    Petec Guest

    Ray Gardener wrote:
    > C++ doesn't allow:
    >
    > class counter : public int {};
    >
    >
    > Was there any special reason why base class ids can only be
    > struct/class ids? This limitation appears to violate the
    > concept of uniform type conceptualization; i.e. classes
    > can never have or extend an is-a relationship to a scalar.
    >
    >
    > Ray Gardener
    > Daylon Graphics Ltd.
    > http://www.daylongraphics.com
    > "Heightfield modeling perfected"


    While that's not allowed, you can do something like:

    class counter
    {
    int base;
    public:
    counter(int b): base(b) {}
    operator int() {return base;}

    /* operators such as + * - / ++ -- etc */
    };

    I believe that has the same effect as the one you want...

    - Pete
     
    Petec, Jun 10, 2004
    #2
    1. Advertising

  3. Ray Gardener wrote:
    > C++ doesn't allow:
    >
    > class counter : public int {};
    >
    >
    > Was there any special reason why base class ids can only be
    > struct/class ids?


    There probably was. Intrinsic types are not classes. The main
    reasons for separating the intrinsic types are the backwards
    compatibility and optimisation.

    > This limitation appears to violate the
    > concept of uniform type conceptualization; i.e. classes
    > can never have or extend an is-a relationship to a scalar.


    Violate the concept? Who said that the concept had to stay intact?
    Besides, where did you find that "concept of uniform type
    conceptualization"? I couldn't find any reference to that on the
    'Net.

    V
     
    Victor Bazarov, Jun 10, 2004
    #3
  4. Ray Gardener

    JKop Guest

    Petec posted:

    > Ray Gardener wrote:
    >> C++ doesn't allow:
    >>
    >> class counter : public int {};
    >>
    >>
    >> Was there any special reason why base class ids can only be
    >> struct/class ids? This limitation appears to violate the
    >> concept of uniform type conceptualization; i.e. classes
    >> can never have or extend an is-a relationship to a scalar.
    >>
    >>
    >> Ray Gardener
    >> Daylon Graphics Ltd.
    >> http://www.daylongraphics.com
    >> "Heightfield modeling perfected"

    >
    > While that's not allowed, you can do something like:
    >
    > class counter
    > {
    > int base;
    > public:
    > counter(int b): base(b) {}
    > operator int() {return base;}
    >
    > /* operators such as + * - / ++ -- etc */
    > };
    >
    > I believe that has the same effect as the one you want...
    >
    > - Pete



    I would suggest piggybacking on the predefined intrinsic int operators, by
    defining an int cast operator:

    counter::eek:perator int(void)
    {
    return base;
    }

    And then, when something tries to mess with your "base" value, which
    ofcourse will be protected, you can interfer:

    counter::eek:perator= (int& sauce)
    {
    if //....

    }
     
    JKop, Jun 10, 2004
    #4
  5. Petec wrote:
    > Ray Gardener wrote:
    >
    >>C++ doesn't allow:
    >>
    >> class counter : public int {};
    >>
    >>
    >>Was there any special reason why base class ids can only be
    >>struct/class ids? This limitation appears to violate the
    >>concept of uniform type conceptualization; i.e. classes
    >>can never have or extend an is-a relationship to a scalar.
    >>
    >>
    >>Ray Gardener
    >>Daylon Graphics Ltd.
    >>http://www.daylongraphics.com
    >>"Heightfield modeling perfected"

    >
    >
    > While that's not allowed, you can do something like:
    >
    > class counter
    > {
    > int base;
    > public:
    > counter(int b): base(b) {}
    > operator int() {return base;}


    Better be
    operator int () const { return base; }

    >
    > /* operators such as + * - / ++ -- etc */
    > };
    >
    > I believe that has the same effect as the one you want...


    For ++ and -- you would also have to add

    operator int&() { return base; }

    Then it _might_ have the sought effect.

    V
     
    Victor Bazarov, Jun 10, 2004
    #5
  6. Ray Gardener

    Petec Guest

    JKop wrote:
    > Petec posted:
    >
    >> Ray Gardener wrote:
    >>> C++ doesn't allow:
    >>>
    >>> class counter : public int {};
    >>>
    >>>
    >>> Was there any special reason why base class ids can only be
    >>> struct/class ids? This limitation appears to violate the
    >>> concept of uniform type conceptualization; i.e. classes
    >>> can never have or extend an is-a relationship to a scalar.
    >>>
    >>>
    >>> Ray Gardener
    >>> Daylon Graphics Ltd.
    >>> http://www.daylongraphics.com
    >>> "Heightfield modeling perfected"

    >>
    >> While that's not allowed, you can do something like:
    >>
    >> class counter
    >> {
    >> int base;
    >> public:
    >> counter(int b): base(b) {}
    >> operator int() {return base;}
    >>
    >> /* operators such as + * - / ++ -- etc */
    >> };
    >>
    >> I believe that has the same effect as the one you want...
    >>
    >> - Pete

    >
    >
    > I would suggest piggybacking on the predefined intrinsic int
    > operators, by defining an int cast operator:
    >
    > counter::eek:perator int(void)
    > {
    > return base;
    > }


    That's exactly what I had, do you mean
    counter::eek:perator int&() {...}
    ?

    - Pete

    >
    > And then, when something tries to mess with your "base" value, which
    > ofcourse will be protected, you can interfer:
    >
    > counter::eek:perator= (int& sauce)
    > {
    > if //....
    >
    > }
     
    Petec, Jun 10, 2004
    #6
  7. Ray Gardener

    Petec Guest

    Victor Bazarov wrote:
    > Petec wrote:
    >> Ray Gardener wrote:
    >>
    >>> C++ doesn't allow:
    >>>
    >>> class counter : public int {};
    >>>
    >>>
    >>> Was there any special reason why base class ids can only be
    >>> struct/class ids? This limitation appears to violate the
    >>> concept of uniform type conceptualization; i.e. classes
    >>> can never have or extend an is-a relationship to a scalar.
    >>>
    >>>
    >>> Ray Gardener
    >>> Daylon Graphics Ltd.
    >>> http://www.daylongraphics.com
    >>> "Heightfield modeling perfected"

    >>
    >>
    >> While that's not allowed, you can do something like:
    >>
    >> class counter
    >> {
    >> int base;
    >> public:
    >> counter(int b): base(b) {}
    >> operator int() {return base;}

    >
    > Better be
    > operator int () const { return base; }
    >


    Yes, thanks for pointing that out. I have a tendency to forget about const.
    ;)

    >>
    >> /* operators such as + * - / ++ -- etc */
    >> };
    >>
    >> I believe that has the same effect as the one you want...

    >
    > For ++ and -- you would also have to add
    >
    > operator int&() { return base; }
    >
    > Then it _might_ have the sought effect.


    Why would it not work with manually defining all overloadable operators for
    it instead?

    - Pete

    >
    > V
     
    Petec, Jun 10, 2004
    #7
  8. Ray Gardener

    Ray Gardener Guest

    > Violate the concept? Who said that the concept had to stay
    intact?
    > Besides, where did you find that "concept of uniform type
    > conceptualization"? I couldn't find any reference to that on

    the
    > 'Net.



    Well, it just strikes me as odd that a scalar is a type, and a
    class is a type, but they can't be uniformly treated in the same
    manner. Class counter can never _be_ an integer; it can only
    _contain_ an integer member.

    On the other hand, the scalars are machine-level types. In the
    future we might see a stdlib header file that has:

    // types.h -- standard types

    class int { ... };
    class float { ... };
    class double { ... };
    ....

    /*
    The traditional scalar typenames are now actually classes and
    can be treated like any other class. The actual machine-level
    numeric scalars are private and compiler/platform dependant.
    If the compiler sees that it can optimize a "scalar" down
    to its machine equivalent, it will do so automatically.
    */

    Then again, that wouldn't work for C code... it'd have to assume
    that C has been obsoleted.

    Ray
     
    Ray Gardener, Jun 10, 2004
    #8
    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. Replies:
    8
    Views:
    338
    Paul Boddie
    Mar 1, 2005
  2. ago
    Replies:
    3
    Views:
    346
  3. Replies:
    2
    Views:
    9,237
    Steve Holden
    Feb 28, 2008
  4. Jeff Thies

    scalars and namespace

    Jeff Thies, Jun 27, 2003, in forum: Perl Misc
    Replies:
    4
    Views:
    105
    Kent Paul Dolan
    Jun 28, 2003
  5. niry
    Replies:
    5
    Views:
    135
Loading...

Share This Page