目录前言什么是协变返回值类型(Covariant)协变返回值类型(Covariant)的作用前言 c++中当子类覆写(override)父类虚函数时,子类函数的返回值类型可以和父类函
c++中当子类覆写(override)父类虚函数时,子类函数的返回值类型可以和父类函数的返回值类型不一致吗?
先说结论:可以,但当且仅当它们的返回值类型是协变返回值类型(Covariant)时可以。C++中GCc从3.4开始支持这一特性。
函数的协变返回值类型指的是子类中的成员函数的返回值类型不必严格等同与该函数所重写的父类中的函数的返回值类型,而可以是更 “狭窄” 的类型。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,因为:
但是我们可以通过上面的代码实现类似的想法,如果我们拥有指向对象的指针:
比如下面的使用场景:
void userCode(Shape* s)
{
Shape* s2 = s->clone();
Shape* s3 = s->create();
// ...
delete s2;
delete s3;
}
如果指针指向的是基类对象,调用上述函数时返回的就是指向基类对象的指针并赋值给s2/s3,如果指针指向的是子类对象,调用上述函数时返回的就是指向子类对象的指针并赋值给s2/s3。
协变返回类型到底有什么用呢,编译器为什么要支持这种语法?如果编译器不支持,上面的例子将只能写成如下这样:
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,失去了动态绑定的意义。
到此这篇关于C++中Covariant返回值类型详解的文章就介绍到这了,更多相关C++ Covariant内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: C++中Covariant返回值类型详解
本文链接: https://www.lsjlt.com/news/167228.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0