免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: 2eye
打印 上一主题 下一主题

[C++] C++中带参数、拷贝构造函数的继承问题。 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2006-04-19 23:21 |只看该作者
关于operator = 的问题,我记不得在哪里看过说把这个函数声明成virtual的时候需要注意一些什么了,改天我头脑清醒一点的时候再找找,昨晚熬夜看球精神有点恍惚了

论坛徽章:
0
12 [报告]
发表于 2006-04-19 23:39 |只看该作者
写了一个测试operator=的小程序,对结果有点不解

  1. #include <iostream>
  2. using namespace std;

  3. class A{
  4. public:
  5.         A(int i) :m_i(i){}
  6.         A& operator=( A& other);
  7.         void print() { cout<<"m_i: "<<m_i<<endl; }
  8. private:
  9.         int m_i;
  10. };

  11. A& A::operator=( A& other )
  12. {       
  13.         cout<<"from A's operator=()"<<endl;
  14.         m_i = other.m_i;
  15.         return *this;
  16. }

  17. class B : public A{
  18. public:
  19.         B(int i1, int i2) :A(i1),new_data(i2){}
  20.         void print();
  21. private:
  22.         int new_data;
  23. };
  24.        
  25. void B::print()
  26. {
  27.         A::print();
  28.         cout<<"new_data: "<<new_data<<endl;
  29. }

  30. int main()
  31. {
  32.         B b1(10,20);
  33.         B b2(40,50);
  34.         cout<<"before operator=()"<<endl;
  35.         b2.print();
  36.         cout<<endl<<"Call operator=()"<<endl<<endl;
  37.         b2 = b1;
  38.         b2.print();
  39.        
  40.         return 0;
  41. }
复制代码

输出为:

  1. before operator=()
  2. m_i: 40
  3. new_data: 50

  4. Call operator=()

  5. from A's operator=()
  6. m_i: 10
  7. new_data: 20

复制代码

既然是调用的A中定义的operator=(),但是A中的operator=()明明没有复制new_data,也不可能知道A的派生类有那些数据需要复制,怎么得到的结果还恰恰正好是对的。难道又继承了A的,编译器还自动加了复制新数据的内容?或者是编译器自动生成了B的operator=的代码,这个代码里调用了基类中(如果有的话)的operator=().

论坛徽章:
0
13 [报告]
发表于 2006-04-19 23:48 |只看该作者
>>>>如Base(int){},但是派生类却没有Derive(int)这样的构造函数;为什么不能继承这个函数。
>>没懂,这两个函数函数名称都不一样了,如何能够在调用int的derive构造的时候找不到定义就去找base里面的呢?这个显然是不合理的.

我的意思是说,从语意上讲,派生来的构造函数至少在一大部分是与基类一样的。那么就算我没有定义Derive(int),编译器也该知道我是想调用Base(int)来初始化派生类的基类部分吧。呵呵,好像要求高了点。

论坛徽章:
0
14 [报告]
发表于 2006-04-20 15:46 |只看该作者

回复 13楼 2eye 的帖子

构造函数好像不能被继承吧?

论坛徽章:
0
15 [报告]
发表于 2006-04-20 19:58 |只看该作者
原帖由 2eye 于 2006-4-19 23:39 发表
写了一个测试operator=的小程序,对结果有点不解
[code]
#include <iostream>
using namespace std;

class A{
public:
        A(int i) :m_i(i){}
        A& operator=( A& other);
        void print() {  ...

这个方法是正确的

在子类的构造函数之前构造

论坛徽章:
0
16 [报告]
发表于 2006-04-20 22:00 |只看该作者
原帖由 net_robber 于 2006-4-20 19:58 发表

这个方法是正确的

在子类的构造函数之前构造

啥意思,没看懂

论坛徽章:
0
17 [报告]
发表于 2006-04-21 10:59 |只看该作者
原帖由 converse 于 2006-4-19 23:21 发表
关于operator = 的问题,我记不得在哪里看过说把这个函数声明成virtual的时候需要注意一些什么了,改天我头脑清醒一点的时候再找找,昨晚熬夜看球精神有点恍惚了:)

operator= is not inherited.  You can verify this by attempting to inherit an operator= some time.  It definitely does not work.  This might cause you to believe that declaring them to be virtual is meaningless, but this is not the case.

If class A has:

        virtual A& operator=(const A&);

and Class B derived from A has

        virtual A& operator=(const A&);

Then the following code will invoke B's operator=:

        A a;
        B b;
        A &r = b;

        r=a; // virtually invokes B's operator=

So, you would want to make operator= any time you thought you might be doing polymorphic assigment through a reference....

Note however that both assignment operators have to have the same signature.  The utility of A& B::operator=(const A&) is unclear, and probably not significant.  The important thing to remember is that

        B& B::operator=(const B&);

Does not override, nor virtually substitute for

        A& A::operator=(const A&);

So, usually, there is not much point in making operator= virtual.

我帮你把表情禁用了,这样方便查看--converse

[ 本帖最后由 converse 于 2006-4-21 12:58 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP