首页 > 其他分享 >协变返回类型(covariant return type)

协变返回类型(covariant return type)

时间:2024-02-22 10:24:36浏览次数:19  
标签:返回 return 函数 covariant 协变 派生类 类型 基类

协变返回类型(covariant return type)

C++ 中的协变返回类型(covariant return type)是指派生类(子类)中的虚函数返回类型可以是基类(父类)中虚函数返回类型的子类。这个特性使得在派生类中可以返回更具体的类型,而不违反了虚函数的约定。

在 C++11 中,如果派生类的虚函数覆盖基类的虚函数,并且派生类的返回类型是基类函数的返回类型的派生类型(或其引用/指针),则可以使用协变返回类型。

示例:

#include <iostream>

class Base {
public:
    virtual Base* clone() const {
        std::cout << "Base::clone()" << std::endl;
        return new Base(*this);
    }
};

class Derived : public Base {
public:
    // 使用协变返回类型,返回类型是 Base 的派生类型 Derived*
    virtual Derived* clone() const override {
        std::cout << "Derived::clone()" << std::endl;
        return new Derived(*this);
    }
};

int main() {
    Derived d;
    Base* ptr = &d;
    Base* newPtr = ptr->clone(); // 调用 Derived::clone()
    delete newPtr;
    return 0;
}

在这个示例中,Derived​ 类覆盖了 Base​ 类的 clone​ 函数,并使用了协变返回类型,返回类型是 Base​ 类的派生类型 Derived*​。这样可以确保在基类指针指向派生类对象时调用的是派生类中的函数,而不是基类中的函数。

image

标签:返回,return,函数,covariant,协变,派生类,类型,基类
From: https://www.cnblogs.com/thatdor/p/18026727/coordinating-return-type-covariant-return-type-

相关文章

  • Go 100 mistakes - #45: Returning a nil receiver
        We’veseeninthissectionthatinGo,havinganilreceiverisallowed,andaninterfaceconvertedfromanilpointerisn’tanilinterface.Forthatreason,whenwehave toreturnaninterface,weshouldreturnnotanilpointerbuta......
  • subprocess中的return_code与poll
    subprocess中的return_code与pollp=subprocess.Popen('ping8.8.8.8',shell=True,stdout=subprocess.PIPE,stderror=subprocess.DEVNULL)whilenotp.poll():#p.poll()即为return_codeprint(p.stdout.read().decode())#return_code=p.poll()#......
  • return vs exit
    在C语言中,return和exit都是用于退出函数的,但它们之间有一些区别。return语句:return语句用于从函数中返回一个值。当函数执行到return语句时,函数立即结束并返回指定的值。return语句可以带一个值,也可以不带值。如果不带值,那么函数返回一个默认值(例如,对于整数函数,返回0;对于浮点函......
  • useEffect中return语句的执行时机
    概要:在开发过程中我发现了一个问题,在useEffect中写的return函数并没有执行,于是在此基础上进行了查证和测试.一、useEffect的使用方法1.两个参数,第二个参数为空数组useEffect(()=>{console.log('111')},[])结果:执行一次2.两个参数,第二个参数不为空数组......
  • [Go] Get used to return (*SomeType, error) as function return type
    packagemainimport( "fmt" "log" "strconv" "strings")typePointstruct{ xint yint}typeLinestruct{ p1*Point p2*Point}funcgetInput()string{return`0,9->5,98,0->0,89,4->......
  • Go 100 mistakes - #7: Returning interfaces
       Allinall,inmostcases,weshouldn’treturninterfacesbutconcreteimplementa-tions.Otherwise,itcanmakeourdesignmorecomplexduetopackagedependencies andcanrestrictflexibilitybecausealltheclientswouldhavetorelyonthesam......
  • 3 return2/14
    #include<bits/stdc++.h>usingnamespacestd;intmain(){intn;while(cin>>n){if(n==0)return0;intans=0,cnt=0;vector<int>a(n);for(auto&num:a){cin>>num;......
  • 使用IDEA直接连接数据库报错:Server returns invalid timezone. Go to 'Advanced' tab
    错误详情:使用IDEA直接连接数据库报错:Serverreturnsinvalidtimezone.Goto'Advanced'tabandset'serverTimezone'propertymanually.错误原因:MySQL驱动中默认时区是UTC,与本地时间有时差。解决方案:点开最右侧导航栏Advanced,找到serverTimezone,在value处填写GMT保存再......
  • yield from 关键字的 return 语句
    我经常需要写一些比较复杂的代码,常常会遇到各种各样的问题。比如我在使用yieldfrom表达式时,return语句的问题。我们知道,在使用yieldfrom表达式时,return语句的作用是在子生成器(被调用的生成器)执行完毕后,返回最终的结果到调用者。这可以让生成器在嵌套结构中更清晰地传递值。......
  • 1 return2/11
     #include<stdio.h>intmain(){inta,b;while(scanf("%d%d",&a,&b)!=EOF){if(a=='\n'||b=='\n')return0;printf("%d\n",a+b);}return0;}关键:......