Chinaunix

标题: 运算符重载的继承多态性如何实现 ? [打印本页]

作者: scum2006    时间: 2014-05-24 01:40
标题: 运算符重载的继承多态性如何实现 ?
本帖最后由 scum2006 于 2014-05-25 11:58 编辑

为什么使用重载的+的时候,TransactionAccount的 Balance没有被传递?



===============================
问题已解决,把父类的operator+ 作为virtual的,两个子类中分别override,就可以实现运算符重载的多态性了



父类:
  1.   BankAccount(float);
  2.   virtual float operator+(BankAccount&);

  3. float BankAccount::operator+( BankAccount &acountIn )
  4. {
  5.         cout << "balance after  \"+\" is  " << acountIn.getBalance()  << endl;
  6.         cout << "balance before \"+\" is  " << getBalance()  << endl;
  7.         return getBalance() + acountIn.getBalance();
  8. }
  9.   
复制代码
两个子类,
  1. SavingsAccount(float);
  2.   float operator+( SavingsAccount & );
  3.   float operator+( BankAccount & );

  4. float SavingsAccount::operator+( SavingsAccount &acountIn )
  5. {
  6.         cout << "balance after  \"+\" is  " << acountIn.getBalance()  << endl;
  7.         cout << "balance before \"+\" is  " << getBalance()  << endl;
  8.         return getBalance() + acountIn.getBalance();
  9. }

  10. float SavingsAccount::operator+( BankAccount &acountIn )
  11. {
  12.         cout << "balance after  \"+\" is  " << acountIn.getBalance()  << endl;
  13.         cout << "balance before \"+\" is  " << getBalance()  << endl;
  14.         return getBalance() + acountIn.getBalance();
  15. }
复制代码

  1. TransactionAccount(float);
  2.   float operator+( TransactionAccount & );
  3.    float operator+( BankAccount & );
  4. float TransactionAccount::operator+( TransactionAccount &acountIn )
  5. {
  6.         cout << "balance after  \"+\" is  " << acountIn.getBalance()  << endl;
  7.         cout << "balance before \"+\" is  " << getBalance()  << endl;
  8.         return getBalance() + acountIn.getBalance();
  9. }

  10. float TransactionAccount::operator+( BankAccount &acountIn )
  11. {
  12.         cout << "balance after  \"+\" is  " << acountIn.getBalance()  << endl;
  13.         cout << "balance before \"+\" is  " << getBalance()  << endl;
  14.         return getBalance() + acountIn.getBalance();
  15. }
复制代码
这样子就两个子类想怎么加就怎么加了
  1.         TransactionAccount * TA2 = new TransactionAccount(800);
  2.         SavingsAccount * SA2 = new SavingsAccount(200);

  3.         float tmp = *TA2 + *TA2;
  4.         cout << "TransactionAccount + TransactionAccount = " << tmp << endl;
  5.         cout << "==========================================" << endl;
  6.         tmp = *SA2 + *SA2;
  7.         cout << "SavingsAccount + SavingsAccount = " << tmp << endl;
  8.         cout << "==========================================" << endl;
  9.         tmp = *SA2 + *TA2;
  10.         cout << "SavingsAccount + TransactionAccount = " << tmp << endl;
  11.         cout << "==========================================" << endl;
  12.         tmp = *TA2 + *SA2;
  13.         cout << "TransactionAccount + SavingsAccount = " << tmp << endl;
复制代码

作者: folklore    时间: 2014-05-24 08:30
多态性是对指针而言的。
作者: scum2006    时间: 2014-05-24 11:29
folklore 发表于 2014-05-24 08:30
多态性是对指针而言的。


所以问题出在我没有传递指针
作者: 幻の上帝    时间: 2014-05-26 10:59
**交换律小心被坑……
作者: windoze    时间: 2014-05-27 16:20
凡是打算在operator上实验多态性的童鞋,多半最后要掉进一个大坑里。
作者: Herowinter    时间: 2014-05-27 18:30
回复 5# windoze
不明真相的路过,大神能简单解释下潜在的问题吗?

   
作者: windoze    时间: 2014-05-28 08:44
回复 6# Herowinter

有两个类
class A{...};
class B : public A{...}

如果有A a; B b;
那么a+b的类型是?

如果有A a; B b=a;
那么a==b吗?

…………

如果你没想清上面这些问题的答案“应该”是什么,那坑已经在你脚下了
作者: Herowinter    时间: 2014-05-28 10:12
回复 7# windoze
多谢耐心解答,我的水平还真不是很确定答案,
我琢磨下再回答你。


   
作者: windoze    时间: 2014-05-28 10:44
本帖最后由 windoze 于 2014-05-28 10:46 编辑

回复 8# Herowinter

这些问题不是语法问题,而是“你该怎么做”的问题。

如果operator+是一个成员函数,A:: operator+(const A& )的返回类型按照惯例应该是一个右值,而且是一个值类型,在这里你唯一的选择就是A(话说此时你正在写A,还没有B呢)
但如果它返回A,那么a+b的返回的就是一个A的实例,也就是说,a+b会先把b“切片”成一个A类型的对象,再进行运算。

如果operator+是全局函数,对于类型A和B,你必须定义:
A operator+(const &A, const &A );
B operator+(const &A, const &B );
如果要满足交换律,你还得有:
B operator+(const &B, const &A );
B operator+(const &B, const &B );
如果B再有个子类C,上述的函数个数就要翻倍,如果C再有个子类D……

到这里,才只解决了一个加号而已,嗯,不说好了还有四则运算相等不等大于小于吗~~~~~~~~~~~~~
作者: Herowinter    时间: 2014-05-28 11:13
回复 9# windoze
这是个陨石坑啊。。。
   
作者: scum2006    时间: 2014-05-30 18:39
windoze 发表于 2014-05-28 10:44
回复 8# Herowinter

这些问题不是语法问题,而是“你该怎么做”的问题。

  1. float SavingsAccount::operator+( SavingsAccount &acountIn )

  2. float SavingsAccount::operator+( BankAccount &acountIn )
复制代码
所以这里的返回值才会是float而不是A 或B啊{:2_168:}
因为目的不是为了相加两个不同子类的对象再返回对象,否则就危险了。
作者: windoze    时间: 2014-05-30 22:32
回复 11# scum2006


如果它返回一个float,那么a+b+c怎么办?a+(b+c)呢?
如果这些Account都能当成float,你还不如给每个类都加一个operator float(),也不用管什么继承多态之类的破事了。
作者: scum2006    时间: 2014-05-31 13:54
windoze 发表于 2014-05-30 22:32
回复 11# scum2006


{:2_168:} you are right, very trouble 的,以前从来没有考虑过运算符重载的多态性,很奇怪,这是一道考题




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2