为什么我不能从C ++继承int? [英] Why can't I inherit from int in C++?
问题描述
我希望能够这样做:
class myInt : public int
{
};
为什么我不能?
为什么我想要?更强的打字。例如,我可以定义两个类 intA
和 intB
,它让我做 intA + intA
或 intB + intB
,但不是 intA + intB
。
Why would I want to? Stronger typing. For example, I could define two classes intA
and intB
, which let me do intA + intA
or intB + intB
, but not intA + intB
.
Ints不是类。那么,什么?
"Ints aren't classes." So what?
Ints没有任何成员数据。是的,他们有32位,或任何。
"Ints don't have any member data." Yes they do, they have 32 bits, or whatever.
Ints没有任何成员函数。嗯,他们有一大堆操作符,例如 +
和 -
。
"Ints don't have any member functions." Well, they have a whole bunch of operators like +
and -
.
推荐答案
Neil的评论是相当准确的。 Bjarne提到考虑并拒绝这个确切的可能性 1 :
Neil's comment is pretty accurate. Bjarne mentioned considering and rejecting this exact possibility1:
初始化语法以前是
适用于内置类型。为了允许
,我引入了一个概念,
内置类型有构造函数和
析构函数。例如:
The initializer syntax used to be illegal for built-in types. To allow it, I introduced the notion that built-in types have constructors and destructors. For example:
int a(1); // pre-2.1 error, now initializes a to 1
我认为将此概念扩展到
允许从内置类
派生,并为内置类型显式声明内置的
操作符。但是,
我限制了自己。
I considered extending this notion to allow derivation from built-in classes and explicit declaration of built-in operators for built-in types. However, I restrained myself.
允许
从 int
派生
实际上给了一个C ++程序员
任何显着新的比
有一个 int
成员。这是
主要是因为 int
没有
任何虚函数用于派生的
类要覆盖。更严重的
虽然,C转换规则是如此
混乱假装 int
,
short
等,都是良好的
普通类是不工作。
它们是C兼容的,或者他们
服从相对良好的C ++
类的规则,但不是两个。
Allowing
derivation from an int
doesn't
actually give a C++ programmer
anything significantly new compared to
having an int
member. This is
primarily because int
doesn't have
any virtual functions for the derived
class to override. More seriously
though, the C conversion rules are so
chaotic that pretending that int
,
short
, etc., are well-behaved
ordinary classes is not going to work.
They are either C compatible, or they
obey the relatively well-behaved C++
rules for classes, but not both.
对于注释,性能证明不会使int是一个类,它(至少大部分)是false。在Smalltalk中,所有类型都是类 - 但是几乎所有Smalltalk的实现都有优化,所以实现可以基本上等同于非类类型的工作。例如,smallInteger类表示一个15位整数,并且'+'消息被硬编码到虚拟机中,因此即使您可以从smallInteger派生,它仍然提供类似于内置类型的性能虽然Smalltalk与C ++的不同之处在于直接的性能比较是困难的,并且不太可能意味着太多)。
As far as the comment the performance justifies not making int a class, it's (at least mostly) false. In Smalltalk all types are classes -- but nearly all implementations of Smalltalk have optimizations so the implementation can be essentially identical to how you'd make a non-class type work. For example, the smallInteger class is represents a 15-bit integer, and the '+' message is hard-coded into the virtual machine, so even though you can derive from smallInteger, it still gives performance similar to a built-in type (though Smalltalk is enough different from C++ that direct performance comparisons are difficult and unlikely to mean much).
编辑:Smalltalk实现smallInteger中的浪费可能不需要在C或C ++中。 Smalltalk有点像Java - 当你定义一个对象,你真的只是定义一个对象的指针,你必须动态分配一个对象来指向。你操作,传递给函数作为参数等,总是只是指针,而不是对象本身。
the one bit that's "wasted" in the Smalltalk implementation of smallInteger probably wouldn't be needed in C or C++. Smalltalk is a bit like Java -- when you "define an object" you're really just defining a pointer to an object, and you have to dynamically allocate an object for it to point at. What you manipulate, pass to a function as a parameter, etc., is always just the pointer, not the object itself.
这不是 如何实现smallInteger虽然 - 在它的情况下,他们将整数值直接放入通常是指针。为了区分smallInteger和指针,它们强制所有对象在偶数字节边界处分配,因此LSB总是清楚的。一个smallInteger总是有LSB集合。
That's not how smallInteger is implemented though -- in its case, they put the integer value directly into what would normally be the pointer. To distinguish between a smallInteger and a pointer, they force all objects to be allocated at even byte boundaries, so the LSB is always clear. A smallInteger always has the LSB set.
但是,大部分都是必要的,因为Smalltalk是动态类型的 - 它必须能够通过查看值本身,而smallInteger基本上使用该LSB作为类型标签。由于C ++是静态类型的,所以从来不需要从值中推导出类型,所以你可能不需要浪费这一点。
Most of this is necessary, however, because Smalltalk is dynamically typed -- it has to be able to deduce the type by looking at the value itself, and smallInteger is basically using that LSB as a type-tag. Given that C++ is statically typed, there's never a need to deduce the type from the value, so you probably wouldn't need to waste that bit.
1 在 的设计和演变,§15.11.3。
1 In The Design and Evolution of C++, §15.11.3.
这篇关于为什么我不能从C ++继承int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!