Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Inheriting scalars

Reply
Thread Tools

Inheriting scalars

 
 
Ray Gardener
Guest
Posts: n/a
 
      06-09-2004
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"


 
Reply With Quote
 
 
 
 
Petec
Guest
Posts: n/a
 
      06-09-2004
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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-09-2004
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
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      06-09-2004
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: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:perator= (int& sauce)
{
if //....

}
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-09-2004
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
 
Reply With Quote
 
Petec
Guest
Posts: n/a
 
      06-09-2004
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:perator int(void)
> {
> return base;
> }


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

- Pete

>
> And then, when something tries to mess with your "base" value, which
> ofcourse will be protected, you can interfer:
>
> counter:perator= (int& sauce)
> {
> if //....
>
> }





 
Reply With Quote
 
Petec
Guest
Posts: n/a
 
      06-09-2004
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




 
Reply With Quote
 
Ray Gardener
Guest
Posts: n/a
 
      06-10-2004
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TypeError: only length-1 arrays can be converted to Python scalars. nure123@gmail.com Python 2 02-28-2008 08:05 PM
Scipy: vectorized function does not take scalars as arguments ago Python 3 05-25-2006 09:22 AM
naming convention for scalars, lists, dictionaries ... beliavsky@aol.com Python 8 03-01-2005 10:11 AM
Why "scalars leaked" in ithread? Is my code right for doing this? niry Perl Misc 5 08-30-2003 07:32 AM
scalars and namespace Jeff Thies Perl Misc 4 06-28-2003 07:30 PM



Advertisments