C++中运算符和函数的区别?

发布于 2024-10-12 03:57:49 字数 251 浏览 8 评论 0原文

我需要一些帮助来理解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

笑忘罢 2024-10-19 03:57:49

运算符是一个符号,如+-+=等(参见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).

梦幻之岛 2024-10-19 03:57:49

什么是运算符?

运算符是在表达式中使用的符号。
示例为: + - * / 等

在内置类型上,操作已明确定义且不可更改。
对于用户定义的类型,可以将运算符定义为函数/方法调用的语法糖

Array a;
a = b + c; // a.operator=(b.operator+(c));

什么是函数?

大多数时候我们可以互换使用术语“函数/方法”。唯一的区别是方法与类对象的实例相关联。否则它们是相同的。它们提供了一种将一组指令分组在一起的方法。

它们有什么区别?

内置类型上的运算符的操作由编译器定义。
运算符对用户定义类型的操作是函数调用。

用户定义的运算符+()是函数还是运算符?

它是一个函数(或方法)。在用户定义类型上使用运算符是函数调用的语法糖。尽管在正常对话中,他们仍然被称为操作员。

运算符可以在编译时对操作数进行操作吗?

对于内置类型是的。编译器具有广泛的能力来优化其使用。
对于用户定义的类型。它可以像其他函数一样对运算符进行优化,这可能会导致被淘汰,但代码不会在编译时执行。

它们总是在编译时运行吗? (类似于 C++ 中的 sizeof())

不。sizeof()是相对独特的。

编辑:

为了显示用户定义的类中的运算符的行为就像函数一样,这里是使用 mem_fun_ref 的示例

#include <vector>
#include <algorithm>
#include <memory>
#include <functional>

class X
{
    public:
        // Non standard operators.
        // Because std::mem_fun_ref has a known weakness in that it can
        // not be used with methods that take parameters be reference.
        //
        // The principle is the same though. That the operator+ can be
        // used anywhere that the add() method can be used.

        X& operator+(X* rhs)  { return *this;}
        X& add(X* rhs)        { return *this;}
};

typedef X& (X::*MEMF)(X* rhs);

int main()
{
    MEMF    p1  = &X::add;
    MEMF    p2  = &X::operator+;

    X               value;
    std::vector<X>  data;

    std::for_each(data.begin(),
                  data.end(),
                  std::bind2nd(std::mem_fun_ref(&X::operator+),&value));
}

What is an operator?

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

Array a;
a = b + c; // a.operator=(b.operator+(c));

What is a function?

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.

What is the difference between them?

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.

Is a user-defined operator+() a function or an operator?

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.

Can an operator operate on operands at compile-time?

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.

Do they always operate at compile time? (like sizeof() in C++)

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

#include <vector>
#include <algorithm>
#include <memory>
#include <functional>

class X
{
    public:
        // Non standard operators.
        // Because std::mem_fun_ref has a known weakness in that it can
        // not be used with methods that take parameters be reference.
        //
        // The principle is the same though. That the operator+ can be
        // used anywhere that the add() method can be used.

        X& operator+(X* rhs)  { return *this;}
        X& add(X* rhs)        { return *this;}
};

typedef X& (X::*MEMF)(X* rhs);

int main()
{
    MEMF    p1  = &X::add;
    MEMF    p2  = &X::operator+;

    X               value;
    std::vector<X>  data;

    std::for_each(data.begin(),
                  data.end(),
                  std::bind2nd(std::mem_fun_ref(&X::operator+),&value));
}
怀里藏娇 2024-10-19 03:57:49

运算符和函数之间没有任何有意义的区别,只是运算符具有不同的语法。然而,原始运算符不是函数。

There is no meaningful difference between operators and functions, except that operators have a different syntax. Primitive operators however are not functions.

べ映画 2024-10-19 03:57:49

什么是运算符?

运算符通常是对给定某种形式的标点符号的变量执行的操作。例如,两个整数之间 operator+ 的默认行为是将它们相加。

什么是函数?

函数是一个子例程——可重用的代码块。

它们有什么区别?

就用户代码而言,除了语法之外什么都没有。请注意,如果您重写 operator||operator&& 或(在较小程度上)operator,,您会更改内置运算符语义。在 &&|| 的情况下,您将通常短路的操作变成非短路的操作。对于逗号,您需要确保从左到右计算参数,因为逗号运算符通常以这种方式运行。

用户定义的operator+()是函数还是运算符?

两者都不。它是用户定义的运算符重载。函数名不能以关键字 operator 开头,运算符只是用于调用运算符重载的实际标点符号,即 +- >。 编辑:请注意,虽然从技术上讲它不是一个函数,但它确实具有函数调用的语义,如 @Martin York 的出色回答

运算符可以在编译时对操作数进行操作吗?它们总是在编译时运行吗? (类似于 C++ 中的 sizeof())

不,sizeof 不能重载。如果您希望完成某种形式的编译时操作,则需要使用诸如模板元编程之类的东西。请注意,如果编译器能够在编译时进行计算,当然它可能会省略对重载运算符的调用。

What is operator?

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.

What is function?

A function is a subroutine -- a reuseable block of code.

What is the difference between them?

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.

Is user-defined operator+() a function or operator?

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.

Can operator operate on operands at compile-time? Do they always operate at compile time? (like sizeof() in C++)

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.

歌入人心 2024-10-19 03:57:49

在 C++ 中,您可以覆盖符号 +、-、== 等应用于类实例时的作用。通过在类 A 中定义“operator+”方法,您可以告诉编译器如何处理以下代码:

A a, b, c;
c = a + b; // the + here actually calls a.operator+(b)

它也是一个函数,或更准确地说是一个实例方法,从某种意义上说,它是被调用的东西。

编辑:另请参阅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:

A a, b, c;
c = a + b; // the + here actually calls a.operator+(b)

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

み青杉依旧 2024-10-19 03:57:49

函数和运算符之间没有太大区别。您可以将 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.

笑脸一如从前 2024-10-19 03:57:49

函数和运算符之间只有两个细微差别。

  1. 运算符可以以两种方式使用(x+yoperator+(a,b))。
  2. 运算符必须具有与内置参数相同数量的参数(operator== 必须恰好具有两个参数)。此规则的例外是函数调用 operator(),它可以使用任意数量的任何参数进行重载。

There are only two minor differences between functions and operators.

  1. Operators can be used in two ways (x+y or operator+(a,b)).
  2. Operators must have same number of parameters as the built-in one (operator== must have exactly two params). The exception from this rule is function call operator() which can be overloaded with any number of any parameters.
等数载,海棠开 2024-10-19 03:57:49

以下是运算符和函数之间的一些区别:

  1. 运算符不会将其参数压入堆栈,但函数会将其参数压入堆栈。

  2. 编译器知道运算符的操作,但不知道函数的输出。换句话说,运算符的操作是在编译时定义的,而函数的操作是在运行时定义的。

Here are some differences between an operator and a function:

  1. An operator does not push its parameters onto the stack, but a function pushes its parameters onto the stack.

  2. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文