运算符重载函数的继承问题

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,所以,“赋值运算符重载函数”不是不能被派生类继承,而是被派生类的默认“赋值运算符重载函数”给覆盖了。

posted @ 2008-07-30 21:22  Conster康  阅读(1933)  评论(0编辑  收藏  举报