使用引用和指针对大对象进行运算符重载
在《The C++Programming Language》一书中,作者给出了以下示例和几个主张。
class Matrix {
double m[4][4];
public:
Matrix( );
friend Matrix operator+(const Matrix&, const Matrix&)
};
Matrix operator+(const Matrix& arg1, const Matrix& arg2)
{
Matrix sum;
for (int i=0; i<4; i++)
sum.m[i][j ]=arg1.m[i][j]+arg2.m[i][j];
return sum;
}
该书声称
引用允许使用涉及大型对象的常用算术运算符的表达式,而无需进行过多的复制。无法使用指针,因为无法重新定义应用于指针的运算符的含义。
我不明白上述声明中的“过度复制”是什么意思。对于“不能使用指针,因为不可能重新定义应用于指针的运算符的含义”的说法,我完全迷失了。感谢您的解释。
In the book of “The C++ Programming Language”, the author gives the following example and several claims.
class Matrix {
double m[4][4];
public:
Matrix( );
friend Matrix operator+(const Matrix&, const Matrix&)
};
Matrix operator+(const Matrix& arg1, const Matrix& arg2)
{
Matrix sum;
for (int i=0; i<4; i++)
sum.m[i][j ]=arg1.m[i][j]+arg2.m[i][j];
return sum;
}
The book claims that
references allow the use of expressions involving the usual arithmetic operators for large objects without excessive copying. Pointers cannot be used because it is not possible to redefine the meaning of an operator applied to a pointer.
I do not understand what does “excessive copying” refer to in the above statement. And for the statement of “Pointers cannot be used because it is not possible to redefine the meaning of an operator applied to a pointer”, I am just totally lost. Thanks for the explanation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果
operator+
被声明为按值获取其操作数,例如,则必须将
arg1
和arg2
的副本传递给的功能。例如,一个简单的矩阵加法需要复制
x
和y
;实际上,您最终必须复制 32 个 double,虽然在本例中不是非常昂贵,但也不是特别便宜。当您通过引用获取参数时,无需复制。请注意,某些运算符重载必须通过引用获取其参数。作为一个常见的示例,请考虑
<<
流插入运算符:它必须通过引用获取其std::istream
操作数,因为无法复制流。不能使用指针,因为不能为指针重载运算符:每个运算符重载的至少一个操作数必须是类或枚举类型。即使你可以使用指针,语法也会很尴尬;您需要使用
&x + &y
,而不是如上所示使用x + y
。If
operator+
was instead declared as taking its operands by value, e.g.,then copies of
arg1
andarg2
would have to be made to be passed into the function. A simple matrix addition, e.g.,would require copies of both
x
andy
to be made; effectively, you end up having to copy 32double
s, which, while not extremely expensive in this case, is not particularly cheap either. When you take an argument by reference, no copy has to be made.Note that some operator overloads must take their argument by reference. As a common example, consider the
<<
stream insertion operator: it must take itsstd::istream
operand by reference because it is impossible to copy the stream.Pointers cannot be used because operators cannot be overloaded for pointers: at least one operand of each operator overload must be a class or enumeration type. Even if you could use pointers, the syntax would be very awkward; instead of using
x + y
as shown above, you would need to use&x + &y
.“过度复制”部分很简单。如果参数列表中没有
&
,则传入时会复制矩阵。引用在某些方面与C++指针类似,它们内部几乎都是使用指针实现的。书中指出的区别之一是,您可以在对类的实例。
但是,您不能执行以下操作(或任何等效语法):
因此您不能编写:
The "excessive copying" part is simple. If there were no
&
in the parameter list, the matrices would be copied when being passed in.References are similar to C++ pointers in some ways, and they are almost always implemented using pointers internally. One of the differences, which the book notes, is that you can use operator overloading on references to an instance of a classs.
However, you can't do the below (or any equivalent syntax):
So you can't write:
不允许用户为内置类型(如指针或整数)定义运算符的原因当然是它们已经拥有由语言为其定义的运算符。
The reason for not allowing user defined operators for built-in types, like pointers or ints, is of course that they already have operators defined for them by the language.