Is there a way to forward declare covariance?

后端 未结 4 1489
别跟我提以往
别跟我提以往 2021-02-19 02:02

Suppose I have these abstract classes Foo and Bar:

class Foo;
class Bar;

class Foo
{
public:
  virtual Bar* bar() = 0;
};

class Bar
{         


        
4条回答
  •  旧时难觅i
    2021-02-19 02:42

    Covariance is based on inheritance diagram, so since you cannot declare

    class ConcreteBar : public Bar;
    

    hence no way to tell compiler about covariance.

    But you can do it with help of templates, declare ConcretFoo::bar as template and later bounding allows you solve this problem

提交回复
热议问题