From: Usenet.9.OkianWarrior on
I'd like to create a new type which is polymorphic to int but has some
different characteristics, something like this:

struct ModuloInt : int { // <-- Note: class
derives from int
ModuloInt(int Value) : int(Value) {};
};

int main(void) {

ModuloInt i = 10;
}

Neither Microsoft C++ 2008 (Express) nor gcc version 4.1 thinks this
is legal. I've looked all over the net, but no one has an example of
this or explains why this type of inheritance won't work.

I can of course make the new class by composition and operator
overloading, but I always thought that the builtin types were
considered classes in C++.

Is there a rule somewhere which states that only user-defined classes
can be inherited from?
From: Usenet.9.OkianWarrior on
On Apr 15, 8:18 pm, Usenet.9.OkianWarr...(a)spamgourmet.com wrote:
> I'd like to create a new type which is polymorphic to int but has some
> different characteristics, something like this:
>
> struct ModuloInt : int { // <-- Note: class
> derives from int
> ModuloInt(int Value) : int(Value) {};
> };
>
> int main(void) {
>
> ModuloInt i = 10;
> }
>
> Neither Microsoft C++ 2008 (Express) nor gcc version 4.1 thinks this
> is legal. I've looked all over the net, but no one has an example of
> this or explains why this type of inheritance won't work.
>
> I can of course make the new class by composition and operator
> overloading, but I always thought that the builtin types were
> considered classes in C++.
>
> Is there a rule somewhere which states that only user-defined classes
> can be inherited from?

The "derives from int" in my post is part of the comment in the
previous line - it's not part of the syntax of the class definition.

Google word-wrapped it for me.

From: Ian Collins on
Usenet.9.OkianWarrior(a)spamgourmet.com wrote:
> I'd like to create a new type which is polymorphic to int but has some
> different characteristics, something like this:
>
> struct ModuloInt : int { // <-- Note: class
> derives from int
> ModuloInt(int Value) : int(Value) {};
> };
>
> int main(void) {
>
> ModuloInt i = 10;
> }
>
No, you can't do this.

> I can of course make the new class by composition and operator
> overloading, but I always thought that the builtin types were
> considered classes in C++.
>
No they are not. Maybe you are thinking of Java?

> Is there a rule somewhere which states that only user-defined classes
> can be inherited from?

See section 10 of the standard - "[looking up a base class name] .. If
the name found is not a class-name, the program is ill-formed."

--
Ian Collins.
From: Andrey Tarasevich on
Usenet.9.OkianWarrior(a)spamgourmet.com wrote:
> I'd like to create a new type which is polymorphic to int but has some
> different characteristics, something like this:
>
> struct ModuloInt : int { // <-- Note: class
> derives from int
> ModuloInt(int Value) : int(Value) {};
> };
>
> int main(void) {
>
> ModuloInt i = 10;
> }
>
> Neither Microsoft C++ 2008 (Express) nor gcc version 4.1 thinks this
> is legal.

Not surprisingly. This is illegal in C++.

> I've looked all over the net, but no one has an example of
> this or explains why this type of inheritance won't work.

It depends on what you mean by "why". Short answer: 'int' is not a class
type and in C++ one can inherit from class types only.

> I can of course make the new class by composition and operator
> overloading, but I always thought that the builtin types were
> considered classes in C++.

They have never been considered classes in C++.

> Is there a rule somewhere which states that only user-defined classes
> can be inherited from?

Only classes can be inherited from, which is stated in the language
standard. From the language point of view, all class types are user-defined.

--
Best regards,
Andrey Tarasevich
From: A. W. Dunstan on
Ian Collins wrote:

> Usenet.9.OkianWarrior(a)spamgourmet.com wrote:
>> I'd like to create a new type which is polymorphic to int but has some
>> different characteristics, something like this:
>>
>> struct ModuloInt : int { // <-- Note: class
>> derives from int
>> ModuloInt(int Value) : int(Value) {};
>> };
>>
>> int main(void) {
>>
>> ModuloInt i = 10;
>> }
>>
> No, you can't do this.
>
>> I can of course make the new class by composition and operator
>> overloading, but I always thought that the builtin types were
>> considered classes in C++.
>>
> No they are not. Maybe you are thinking of Java?
>
Even in Java you can't derive from int - that's a 'native data type', and
not a class. "Integer" is a class, but since it's 'final' you can't derive
from it, either. Ruby and C# (and other) will let you derive from int, as
they treat int (and float, and double, etc) as full-fledged objects.


--
Al Dunstan, Software Engineer
OptiMetrics, Inc.
3115 Professional Drive
Ann Arbor, MI 48104-5131