Talk About Network

Google





Programming > C - C++ Learning > Re: Inheriting ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 5 Topic 4117 of 4400
Post > Topic >>

Re: Inheriting from built-in types

by Andrey Tarasevich <andreytarasevich@[EMAIL PROTECTED] > Apr 16, 2008 at 04:27 PM

Usenet.9.OkianWarrior@[EMAIL PROTECTED]
 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 cl***** in C++.

They have never been considered cl***** in C++.

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

Only cl***** 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
 




 5 Posts in Topic:
Inheriting from built-in types
Usenet.9.OkianWarrior@[EM  2008-04-15 20:18:57 
Re: Inheriting from built-in types
Usenet.9.OkianWarrior@[EM  2008-04-15 20:21:51 
Re: Inheriting from built-in types
Ian Collins <ian-news@  2008-04-16 16:18:20 
Re: Inheriting from built-in types
"A. W. Dunstan"  2008-04-21 09:58:22 
Re: Inheriting from built-in types
Andrey Tarasevich <and  2008-04-16 16:27:24 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
localhost-V2008-12-19 Wed Jan 7 13:12:01 PST 2009.