目录
  • 前言
  • 什么是协变返回值类型(Covariant)
  • 协变返回值类型(Covariant)的作用

前言

C++中当子类覆写(override)父类虚函数时,子类函数的返回值类型可以和父类函数的返回值类型不一致吗?
先说结论:可以,但当且仅当它们的返回值类型是协变返回值类型(Covariant)时可以。C++中gcc从3.4开始支持这一特性。

什么是协变返回值类型(Covariant)

函数的协变返回值类型指的是子类中的成员函数的返回值类型不必严格等同与该函数所重写的父类中的函数的返回值类型,而可以是更 “狭窄” 的类型。C++/Java等面向对象编程语言均支持协变返回值类型。

例子:

class Shape {
public:
  virtual ~Shape() { }          
  virtual Shape* clone()  const = 0;   // Uses the copy constructor
  virtual Shape* create() const = 0;   // Uses the default constructor
};
class Circle : public Shape {
public:
  Circle* clone()  const;   // Covariant Return Types; see below
  Circle* create() const;   // Covariant Return Types; see below
 
};
Circle* Circle::clone()  const { return new Circle(*this); }
Circle* Circle::create() const { return new Circle();      }

C++中不支持virtual constructor,因为:

  • 创建对象时需要知道对象的完整信息
  • 虚函数机制也决定了对象尚未创建时,类的virtual table或许还不存在
  • 我们不可能有指向virtual constructor的指针

但是我们可以通过上面的代码实现类似的想法,如果我们拥有指向对象的指针:

  • 通过clone()调用对象的拷贝构造函数,复制当前的对象
  • 通过create()调用默认构造函数,创建新的对象

比如下面的使用场景:

void userCode(Shape* s)
{
  Shape* s2 = s->clone();
  Shape* s3 = s->create();
  // ...
  delete s2;   
  delete s3;
}

如果指针指向的是基类对象,调用上述函数时返回的就是指向基类对象的指针并赋值给s2/s3,如果指针指向的是子类对象,调用上述函数时返回的就是指向子类对象的指针并赋值给s2/s3。

协变返回值类型(Covariant)的作用

协变返回类型到底有什么用呢,编译器为什么要支持这种语法?如果编译器不支持,上面的例子将只能写成如下这样:

class Shape {
public:
  virtual ~Shape() { }          
  virtual Shape* clone()  const = 0;   // Uses the copy constructor
  virtual Shape* create() const = 0;   // Uses the default constructor
};
class Circle : public Shape {
public:
  Shape* clone()  const;   // Covariant Return Types; see below
  Shape* create() const;   // Covariant Return Types; see below
 
};
Shape* Circle::clone()  const { return new Circle(*this); }
Shape* Circle::create() const { return new Circle();      }

这样上面的userCode函数将不能通过编译,上面调用clone函数部分将不得不改写成下面这样:

 void userCode(Shape* s)
{
  Shape* s2 = s->clone();
  Circle* c = dynamic_cast<Circle*>(s2);
  if (c != NULL) {
     // c point to Circle
  } else {
	  if (s2 != NULL) {
	     // s2 point to base Shape
	     }
	  }
  }
  // ...
  delete s2;   
}

通过if/else分支来判断s是指向子类Circle还是指向基类Shape,失去了动态绑定的意义。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。