C++中运算符和函数的区别?
我需要一些帮助来理解 C++ 中的以下内容,特别是运算符和函数之间的区别:
- 什么是运算符?
- 什么是函数?
- 它们之间有什么区别?
- 用户定义的
operator+()
是函数还是运算符? - 运算符可以在编译时对操作数进行操作吗?它们总是在编译时运行吗? (类似于 C++ 中的
sizeof()
)
I could use some help understanding the following in C++, particularly the difference between an operator and a function:
- What is an operator?
- What is a function?
- What is the difference between them?
- Is a user-defined
operator+()
a function or an operator? - Can an operator operate on operands at compile-time? Do they always operate at compile time? (like
sizeof()
in C++)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
运算符是一个符号,如
+
、-
、+=
等(参见13.5)。它们没有任何意义。在语义分析过程中,确定运算符的含义。函数是构造函数、析构函数、转换函数(看起来像
operator type()
)或运算符函数(函数模板专门化和实例化可以依次产生这些)。运算符函数是实现运算符的东西(参见13.5)。一个例子是
operator+
。这些函数在各个方面都是函数,与“通常”函数的唯一区别是它们可以被隐式调用并且它们有一个有趣的名称。有些运算符具有内置含义,可以由程序员更改。人们简单地通过说内置运算符来指代运算符的内置含义(参见5/3)。然而,如果这样的运算符应用于定义了内置含义的操作数,则仅在少数情况下允许更改该含义(这些是赋值、取址和逗号运算符,请参阅 13.5/6)。
An operator is a symbol like
+
,-
,+=
and so forth (see 13.5). They don't carry a meaning. During semantic analysis, the meaning of an operator is determined.A function is a constructor, destructor, conversion function (that looks like
operator type()
) or operator function (function template specialization and instantiation can yield these in turn).An operator function is something that implements an operator (see 13.5). An example is
operator+
. These are functions in all respects, and the only difference to "usual" functions is that they may be called implicitly and they have a funny name.Some operators have a built-in meaning, that can be changed by the programmer. One refers to the built-in meaning of an operator simply by saying built-in operator (see 5/3). However, if such an operator is applied on operands for which a built-in meaning is defined, changing that meaning is only allowed for a few cases (these are assignment, address-of and the comma operator, see 13.5/6).
运算符是在表达式中使用的符号。
示例为: + - * / 等
在内置类型上,操作已明确定义且不可更改。
对于用户定义的类型,可以将运算符定义为函数/方法调用的语法糖
大多数时候我们可以互换使用术语“函数/方法”。唯一的区别是方法与类对象的实例相关联。否则它们是相同的。它们提供了一种将一组指令分组在一起的方法。
内置类型上的运算符的操作由编译器定义。
运算符对用户定义类型的操作是函数调用。
它是一个函数(或方法)。在用户定义类型上使用运算符是函数调用的语法糖。尽管在正常对话中,他们仍然被称为操作员。
对于内置类型是的。编译器具有广泛的能力来优化其使用。
对于用户定义的类型。它可以像其他函数一样对运算符进行优化,这可能会导致被淘汰,但代码不会在编译时执行。
不。sizeof()是相对独特的。
编辑:
为了显示用户定义的类中的运算符的行为就像函数一样,这里是使用 mem_fun_ref 的示例
An operator is a symbol that is use in expressions.
Examples are: + - * / etc
On built-in types there operations are well defined and unchangable.
For user defined types the operators can be defined as syntactic sugar for a function/method call
We use the term function/method interchangeably most of the time. The only difference is that a method is associated with an instance of a class object. Otherwise they are the same. They provide a way of grouping a set of instructions together.
The action of an operator on a built-in type is defined by the compiler.
The action of an operator on a user defined type is a function call.
Its a function (or a method). Use of an operator on a user defined type is syntactic sugar for a function call. They are still refereed to as operators though in normal conversation.
For built-in types yes. The compiler has extensive ability to optimize there usage.
For user defined types. It can perform optimizations on the operators just like other functions which may lead to there being eliminated, but the code is not executed at compile time.
No. sizeof() is relatively unique.
Edit:
To show that operator in user defined class behave just like functions here is an example of using mem_fun_ref
运算符和函数之间没有任何有意义的区别,只是运算符具有不同的语法。然而,原始运算符不是函数。
There is no meaningful difference between operators and functions, except that operators have a different syntax. Primitive operators however are not functions.
运算符通常是对给定某种形式的标点符号的变量执行的操作。例如,两个整数之间
operator+
的默认行为是将它们相加。函数是一个子例程——可重用的代码块。
就用户代码而言,除了语法之外什么都没有。请注意,如果您重写
operator||
、operator&&
或(在较小程度上)operator,
,您会更改内置运算符语义。在&&
和||
的情况下,您将通常短路的操作变成非短路的操作。对于逗号,您需要确保从左到右计算参数,因为逗号运算符通常以这种方式运行。两者都不。它是用户定义的运算符重载。函数名不能以关键字
operator
开头,运算符只是用于调用运算符重载的实际标点符号,即+
或-
>。 编辑:请注意,虽然从技术上讲它不是一个函数,但它确实具有函数调用的语义,如 @Martin York 的出色回答。不,
sizeof
不能重载。如果您希望完成某种形式的编译时操作,则需要使用诸如模板元编程之类的东西。请注意,如果编译器能够在编译时进行计算,当然它可能会省略对重载运算符的调用。An operator is generally an operation performed on a variable given some form of punctuation. For example, the default behavior of
operator+
between two integers is to add them.A function is a subroutine -- a reuseable block of code.
Nothing, as far as user code is concerned, except for syntax. Note that if you override
operator||
,operator&&
, or (to a lesser extent)operator,
, you change the semantics of the built in operator semantics. In the case of&&
and||
, you make the operation which is normally short circuiting into an operation which is not. In the case of the comma, you would need to ensure that you evaluate the arguments left to right, as the comma operator normally behaves in this way.Neither. It is a user defined operator overload. A function name cannot start with the keyword
operator
, and an operator is simply the actual punctuation mark used to invoke the operator overload, i.e.+
or-
. EDIT: Note that while technically speaking it is not a function, it does have the semantics of a function call, as demonstrated in @Martin York's excellent answer.No,
sizeof
cannot be overloaded. If you want some form of compile time operation done, you need to use something like template metaprogramming. Note that if the compiler is able to do the calculation at compile time it may elide the call into your overloaded operator, of course.在 C++ 中,您可以覆盖符号 +、-、== 等应用于类实例时的作用。通过在类 A 中定义“operator+”方法,您可以告诉编译器如何处理以下代码:
它也是一个函数,或更准确地说是一个实例方法,从某种意义上说,它是被调用的东西。
编辑:另请参阅http://en.wikipedia.org/wiki/Operator_overloading
和 http://en.wikibooks.org/wiki/C++_Programming/运算符/Operator_Overloading
In C++ you can override what the symbols +, -, ==, etc. do when applied to class instances. By defining the "operator+" method in class A, you are telling the compiler what to do with code like:
It's also a function or more precisely an instance method, in the sense that it's something that gets called.
EDIT: see also http://en.wikipedia.org/wiki/Operator_overloading
and http://en.wikibooks.org/wiki/C++_Programming/Operators/Operator_Overloading
函数和运算符之间没有太大区别。您可以将 using 运算符(例如“a+b”)视为针对 a 和 b 类型定义的函数运算符+(a,b) 的快捷方式。当然,基本类型(如整数)上的运算符和一些其他异常不一定是这样定义的。
因此,回答您的一些具体问题:
用户定义的运算符+()是函数还是运算符?
实现运算符的函数。
运算符可以在编译时对操作数进行操作吗?它们总是在编译时运行吗?
由于它是一个函数,因此它在运行时运行,但在某些情况下,编译器优化可以在编译时对某些运算符起作用。我不是 100% 确定你为什么问这个,所以也许有一些我不知道的事情。
There is no huge difference between functions and operators. You can think of an using operator, e.g., 'a+b', as a shortcut to the function operator+(a,b) which is defined for the types of a and b. Of course, operators on primitive types (like integers) and a few other exceptions are not necessarily defined like this.
Thus, to answer a few of your specific questions:
Is user-defined operator+() a function or operator?
A function that implements an operator.
Can operator operate on operands at compile-time? Do they always operate at compile time?
Since it is a function, it operates at run time, but in some cases compiler optimizations can do work at compile time for certain operators. I'm not 100% sure why you're asking this, so perhaps there is something I'm not aware of here.
函数和运算符之间只有两个细微差别。
x+y
或operator+(a,b)
)。operator==
必须恰好具有两个参数)。此规则的例外是函数调用operator()
,它可以使用任意数量的任何参数进行重载。There are only two minor differences between functions and operators.
x+y
oroperator+(a,b)
).operator==
must have exactly two params). The exception from this rule is function calloperator()
which can be overloaded with any number of any parameters.以下是运算符和函数之间的一些区别:
运算符不会将其参数压入堆栈,但函数会将其参数压入堆栈。
编译器知道运算符的操作,但不知道函数的输出。换句话说,运算符的操作是在编译时定义的,而函数的操作是在运行时定义的。
Here are some differences between an operator and a function:
An operator does not push its parameters onto the stack, but a function pushes its parameters onto the stack.
The compiler knows about the operation of the operators, but is not aware of the output of the function. Said a different way, the action of the operator is defined at compilation time and that of a function is defined at the runtime.