zoukankan      html  css  js  c++  java
  • 运算符重载函数的继承问题

    operator=之外的所有基类运算符都会自动继承,但它们操作的都是子类中“基类的成员”,即,如果XY都有int成员i,(Y没有重写++这个操作符)则Y y;++y;加的是X中的i,而不是y中的i同理operator==的意义也不会检测y中的i。

    #include <iostream>

    using namespace std;

    class A1

    {

    public:

    int i;

    A1():i(1){}

    public:

    int operator=(int a)

    {

    return 8;

    }

    int operator+(int a)

    {

    return i++;

    }

    };

    class B1 : public A1

    {

    public:

      int operator+(float a)   //这地方虽然参数为float类型,但也构成基类函数的隐藏!

      {

      return i++;

      }

    int operator-(int a)

    {

    return i--;

    }

    B1():i(6){}

    int i;

    };

    int main()

    {        

    B1 v;

    cout<<v.i<<endl<<v.A1::i<<endl; //

    cout << (v + 2) << endl// OK, print 6

    cout << (v - 2) << endl// OK, print 7

    //cout << (v = 2) << endl; // Error, see below

    return 0;

    }

     

    赋值运算符重载函数”不能被继承

     

    1,每一个类对象实例在创建的时候,如果用户没有定义“赋值运算符重载函数”,那么,编译器会自动生成一个隐含和默认的“赋值运算符重载函数”。所以,B1的实际上的声明应该类似于下面这种情况:

    class A1
    {
    public:
             int operator=(int a)
             {
                     return 8;
             }

             int operator+(int a)
             {
                     return 9;
             }
    };

    class B1 : public A1
    {
    public:
             B1& operator =(const B1& robj); // 注意这一行是编译器添加的
             int operator-(int a)
             {
                     return 7;
             }
    };

    2,C++标准规定:如果派生类中声明的成员与基类的成员同名,那么,基类的成员会被覆盖,哪怕基类的成员与派生类的成员的数据类型和参数个数都完全不同。显然,B1中的赋值运算符函数名operator =和基类A1中的operator =同名,所以,A1中的赋值运算符函数int operator=(int a);被B1中的隐含的赋值运算符函数B1& operator =(const B1& robj);所覆盖。 A1中的int operator=(int a);函数无法被B1对象访问。

    3,程序中语句v = 2实际上相当于v.operator =(2);,但是A1中的int operator=(int a);已经被覆盖,无法访问。而B1中默认的B1& operator =(const B1& robj);函数又与参数2的整数类型不相符,无法调用。

    4,为了确认B1中默认的B1& operator =(const B1& robj);函数的存在性,可以用以下代码验证:
    B1 b;
    B1 v;
    v = b; // OK, 相当于调用v.operator =(b);
    5,所以,“赋值运算符重载函数”不是不能被派生类继承,而是被派生类的默认“赋值运算符重载函数”给覆盖了。

  • 相关阅读:
    基于学习的超分辨率技术
    图像缩放技术
    cifar-10 图片可视化
    python图像插值
    替换空格
    配置Windows Server 2008/2012/2016允许多个用户同时远程桌面
    安装XPS文件查看器的方法
    win10外接显示器时有些应用和里面的字体显示比较模糊
    关于中行长城跨境通卡的网上支付常见问题&支付实例
    分布式中Redis实现Session(将Session保存到Redis)
  • 原文地址:https://www.cnblogs.com/hellohuan/p/1256801.html
Copyright © 2011-2022 走看看