JBTALKS.CC

标题: 急!谁知道overloading和addition的结合怎么写? [打印本页]

作者: daphnechen    时间: 2010-7-25 11:35 AM
标题: 急!谁知道overloading和addition的结合怎么写?
overloading和addition的结合怎么写?
有谁会?可以帮我下吗~
一开始要怎么做?
我完全不会~谁可以教教我?
拜托~谢谢~
作者: daphnechen    时间: 2010-7-25 12:08 PM
真的没有人会吗??
救命啊~!@@
作者: shippo    时间: 2010-7-26 05:45 PM
你是要用overloading operators for +(addition) 吧??
在什么language?目的是什么麻烦说出来。。。
作者: shippo    时间: 2010-7-26 05:57 PM
本帖最后由 shippo 于 2010-7-26 05:59 PM 编辑

  1. /*
  2. 给你一个C++的例子。。。。
  3. 这里我用overload + 来取得两个vector(a and b)的和。
  4. */
  5. #include <iostream>
  6. using namespace std;

  7. class CVector {
  8.   public:
  9.     int x,y;
  10.     CVector () {};
  11.     CVector (int,int);
  12.     CVector operator + (CVector);
  13. };

  14. CVector::CVector (int a, int b) {
  15.   x = a;
  16.   y = b;
  17. }

  18. CVector CVector::operator+ (CVector param) {
  19.   CVector temp;
  20.   temp.x = x + param.x;
  21.   temp.y = y + param.y;
  22.   return (temp);
  23. }

  24. int main () {
  25.   CVector a (3,1);
  26.   CVector b (1,2);
  27.   CVector c;
  28.   c = a + b;
  29.   cout << c.x << "," << c.y<<endl;
  30.   system("pause");
  31.   return 0;
  32. }
复制代码

作者: shippo    时间: 2010-7-26 06:02 PM
+ 是一个binary operator ,declare的方法如下:
(下面转载自msdn microsoft)
ret-type operatorop( arg )

where ret-type is the return type, op is one of the operators listed in the preceding table, and arg is an argument of any type.

To declare a binary operator function as a global function, you must declare it in the form:

ret-type operatorop( arg1, arg2 )

where ret-type and op are as described for member operator functions and arg1 and arg2 are arguments. At least one of the arguments must be of class type.
作者: calvenyow    时间: 2010-9-12 10:49 PM
我只懂function overloading
作者: kampung30    时间: 2011-5-2 03:57 AM
overloading很多种咯 ,那里一个?




欢迎光临 JBTALKS.CC (https://www.jbtalks.cc/) Powered by Discuz! X2.5