473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Inheriting scalars

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"
Jul 22 '05 #1
7 1401
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
Jul 22 '05 #2
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::operator int(void)
{
return base;
}

And then, when something tries to mess with your "base" value, which
ofcourse will be protected, you can interfer:

counter::operator= (int& sauce)
{
if //....

}
Jul 22 '05 #3
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
Jul 22 '05 #4
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
Jul 22 '05 #5
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::operator int(void)
{
return base;
}


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

- Pete

And then, when something tries to mess with your "base" value, which
ofcourse will be protected, you can interfer:

counter::operator= (int& sauce)
{
if //....

}



Jul 22 '05 #6
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


Jul 22 '05 #7
> 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
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: richardlane | last post by:
Hi, basic question here - I'm struggling with the transfer from asp to asp.net a bit, especially in seeing the 'bigger picture' of how things are best structured. I have a website made up...
15
by: JustSomeGuy | last post by:
this doesn't want to compile.... class image : public std::list<element> { element getElement(key k) const { image::iterator iter; for (iter=begin(); iter != end(); ++iter) { element...
29
by: shaun roe | last post by:
I want something which is very like a bitset<64> but with a couple of extra functions: set/get the two 32 bit words, and conversion to unsigned long long. I can do this easily by inheriting from...
1
by: john diss | last post by:
hello there everyone.. I have created a class called "ProcessLog" inheriting from XmlDocument and two classes ("UploadedItem", "ProcessError") inheriting from XmlElement. I then have two...
2
by: Peter Bates | last post by:
Hi, I'm just getting used to XSDObjectGen and i have the following question. Can i use a class inherited from a class generated by XSDObjectGen with XmlSerialize? Specifically, I have many...
11
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain...
2
by: Charles Law | last post by:
I want a set of controls that all have a border, like a group box. I thought I would create a base control containing just a group box from which my set of controls could inherit. Assuming that...
3
by: Alex Satrapa | last post by:
There's some mention in the (old!) documentation that constraints such as foreign keys won't include data from inheriting tables, eg: CREATE TABLE foo ( id SERIAL PRIMARY KEY ); CREATE TABLE...
3
by: ago | last post by:
Once I vectorize a function it does not acccept scalars anymore. Es def f(x): return x*2 vf = vectorize(f) print vf(2) AttributeError: 'int' object has no attribute 'astype' Is this the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.