Difference between operator and function in C++?

后端 未结 8 599
夕颜
夕颜 2021-02-01 08:31

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 f
8条回答
  •  自闭症患者
    2021-02-01 09:04

    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

提交回复
热议问题