我想知道为什么 C++ 不支持下面示例中的参数协方差,或者是否有办法实现它?
class base {
public:
virtual base* func(base * ptr) { return new base(); }
};
class derived : public base {
public:
virtual derived* func(derived * ptr) override { return new derived(); } //not allowed
};
我想知道为什么 C++ 不支持下面示例中的参数协方差,或者是否有办法实现它?
class base {
public:
virtual base* func(base * ptr) { return new base(); }
};
class derived : public base {
public:
virtual derived* func(derived * ptr) override { return new derived(); } //not allowed
};