Topic: Is subclassing built-in types allowed ?
Author: Niels Basjes <Basjes@nlr.nl>
Date: 1998/02/11 Raw View
Hi,
Is it allowed (according to the C++ standard) to create a subclass
of a built-in type (for example "long", "int", "float", etc.) ?
The reason I ask is that with G++ 2.7.2 I get a compile error :
"parse error before `long'" when I try compile the next line.
class SpecialLong : public long {};
Can anyone help me on this subject ?
--
Best regards,
Niels.
================================================================
| ir. Niels Basjes | National Aerospace Laboratory |
| Phone : +31-20-5113626 | Anthony Fokkerweg 2 |
| E-Mail@NLR : Basjes@NLR.nl | 1059 CM Amsterdam |
| E-Mail@Home: Niels@Basjes.nl | The Netherlands |
================================================================
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Edward Diener <eddielee@abraxis.com>
Date: 1998/02/12 Raw View
I believe you mean "inherit from" rather than whatever the term "subclass" is
supposed to be. Although I can not quote chapter and verse from the C++
standard, my understanding is that you can only derived a class ( or struct )
from another class ( or struct ), not from a built-in type.
Niels Basjes wrote:
> Hi,
>
> Is it allowed (according to the C++ standard) to create a subclass
> of a built-in type (for example "long", "int", "float", etc.) ?
>
> The reason I ask is that with G++ 2.7.2 I get a compile error :
> "parse error before `long'" when I try compile the next line.
>
> class SpecialLong : public long {};
>
> Can anyone help me on this subject ?
>
> --
>
> Best regards,
>
> Niels.
>
> ================================================================
> | ir. Niels Basjes | National Aerospace Laboratory |
> | Phone : +31-20-5113626 | Anthony Fokkerweg 2 |
> | E-Mail@NLR : Basjes@NLR.nl | 1059 CM Amsterdam |
> | E-Mail@Home: Niels@Basjes.nl | The Netherlands |
> ================================================================
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/02/12 Raw View
Niels Basjes wrote:
>
> Hi,
>
> Is it allowed (according to the C++ standard) to create a subclass
> of a built-in type (for example "long", "int", "float", etc.) ?
>
> The reason I ask is that with G++ 2.7.2 I get a compile error :
> "parse error before `long'" when I try compile the next line.
>
> class SpecialLong : public long {};
The standard types are not classes, so you cannot derive from them.
Instead, you must declare a class which stores its value somehow (in a
long?), and define all of the operators that normally apply to 'long'.
Don't forget these two:
template<type T> T& SpecialLong::operator[](T *pT) const;
template<type T> const T&
SpecialLong::operator[](const T *pT) const;
:-)
At one point, I felt that there should have been a built-in (and
extensible) class hierarchy for C++ convering the standard numeric
types:
numeric
integral
unsigned
uchar
ushort
uint
ulong
signed
schar
short
int
long
floating
float
double
ldouble
However, grafting such a system onto a language that had to be backward
compatible with 'C' is just too complicated. Getting it to work as
desired without adding a 'vtbl' to 'numeric' was one difficulty, as I
remember. Most of the cases where I wanted to have a function with a
'numeric' argument or return type, I would now write using template
parameters. The numeric_limits template is also a big help.
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]