Topic: Inheriting from builtin


Author: Baudouin Raoult <mab@ecmwf.int>
Date: 1996/07/14
Raw View
Hi,

has it ever been considered to inherit from builtin types?

I have two classes Length and Offset that are numbers:

typedef unsigned long Length;
typedef unsigned long Offset;

unfortunatly, as far as the compiler is concerned, they
are identical. What about

class Length : public unsigned long {
};

class Offset : public unsigned long {
};

Then both classes have the benefit of builtin operators but
are different, specially for parameter passing.

Baudouin

--

---------------------------------------------------
Baudouin Raoult.
European Center for Medium Range Weather Forecast
Reading, UK
---------------------------------------------------
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: scotty@cinenet.net (J Scott Peter XXXIII i/iii)
Date: 1996/07/15
Raw View
In article <31E107C8.41C6@ecmwf.int>, Baudouin Raoult <mab@ecmwf.int> wrote:
> has it ever been considered to inherit from builtin types?
>
> I have two classes Length and Offset that are numbers:
>
> typedef unsigned long Length;
> typedef unsigned long Offset;
>
> unfortunatly, as far as the compiler is concerned, they
> are identical. What about
>
> class Length : public unsigned long {
> };
>
> class Offset : public unsigned long {
> };
>
> Then both classes have the benefit of builtin operators but
> are different, specially for parameter passing.
>

Wonderful idea.  BS has stated in "Design and Evolution" that one of the
goals for refining C++ was that builtin types should behave as closely as
possible like classes; they should not be "special".  That's why builtin
types now have constructors: e.g. ``int x(7)'' is the same as ``int x = 7''.

Inheriting from builtin types follows that same principle.  Plus it would
provide type-checking and overloading differentiation.  Plus it would make a
lot of things easier.  Defining a class, for example, that behaved just like
an int, but had a special behavior in some cases.

One might object that one can do this already in the following way:

class PseudoInt {
        int i;
public:
        PseudoInt(int ii): i(ii) {}
        PseudoInt& operator =(int ii)  { return i = ii; }
        operator int()  { return i; }
        operator int&()  { return i; }
};

This would be tedious if it worked, but it doesn't even work.  Simply
providing conversion operators to and from int does *not* make the class
behave like an int.  At least in MS VC4.1, the conversions are not invoked
in many operator expressions, such as x + y, or x += y.


---
John Scott Peter XXXIII i/iii  // Wrong thinking is punishable.
Software Engineer             // Right thinking will be as quickly rewarded.
Venice, CA                   // You will find it an effective combination.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]