Facebook Sharer
选择您要替换的背景颜色:
【农历新年】背景图片:
个性化设定
 注册  找回密码
查看: 1137|回复: 6
打印 上一主题 下一主题

急!谁知道overloading和addition的结合怎么写?

[复制链接]

39

主题

4

好友

1170

积分

黄金长老

Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8

跳转到指定楼层
1#
发表于 2010-7-25 11:35 AM |只看该作者 |倒序浏览
overloading和addition的结合怎么写?
有谁会?可以帮我下吗~
一开始要怎么做?
我完全不会~谁可以教教我?
拜托~谢谢~




收藏收藏0

39

主题

4

好友

1170

积分

黄金长老

Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8

2#
发表于 2010-7-25 12:08 PM |只看该作者
真的没有人会吗??
救命啊~!@@


回复

使用道具 举报

2

主题

0

好友

251

积分

支柱会员

Rank: 4Rank: 4Rank: 4Rank: 4

3#
发表于 2010-7-26 05:45 PM |只看该作者
你是要用overloading operators for +(addition) 吧??
在什么language?目的是什么麻烦说出来。。。


回复

使用道具 举报

2

主题

0

好友

251

积分

支柱会员

Rank: 4Rank: 4Rank: 4Rank: 4

4#
发表于 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. }
复制代码


回复

使用道具 举报

2

主题

0

好友

251

积分

支柱会员

Rank: 4Rank: 4Rank: 4Rank: 4

5#
发表于 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.


回复

使用道具 举报

0

主题

0

好友

15

积分

初级会员

Rank: 1

6#
发表于 2010-9-12 10:49 PM |只看该作者
我只懂function overloading


回复

使用道具 举报

26

主题

49

好友

1964

积分

白银长老

Rank: 9Rank: 9Rank: 9Rank: 9Rank: 9Rank: 9Rank: 9Rank: 9Rank: 9

7#
发表于 2011-5-2 03:57 AM |只看该作者
overloading很多种咯 ,那里一个?


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2024-10-27 07:16 AM , Processed in 0.113286 second(s), 27 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

Ultra High-performance Dedicated Server powered by iCore Technology Sdn. Bhd.
Domain Registration | Web Hosting | Email Hosting | Forum Hosting | ECShop Hosting | Dedicated Server | Colocation Services
本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved
合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部