z****e 发帖数: 2024 | 1 class overb{
public:
virtual overb f(){}//invalide covariant type
};
class overd:public overb{
public:
virtual overd f(){}//invalide covariant type
};
再看:
class overb{
public:
virtual overb* f(){}//valide covariant type
};
class overd:public overb{
public:
virtual overd* f(){}//valide covariant type
};
难道overd 不是 overb吗? 这不正好满足type 的 <=关系吗?
1. overd 对应 overb
2. overd* 对应 overb*
请问1, 和 2 , 有什么不一样?为什么一个是Covariant Return Type,另一个不是呢
? |
X****r 发帖数: 3557 | 2 Because an "overd *" can be used in place of an "overb *",
but an "overd" cannot be used directly in place of an "overb".
(check Liskov substitution principle)
Think about this:
overd d;
overb b = d;
And this:
overd* pd;
overb* pb = pd;
They are totally different.
【在 z****e 的大作中提到】 : class overb{ : public: : virtual overb f(){}//invalide covariant type : }; : class overd:public overb{ : public: : virtual overd f(){}//invalide covariant type : }; : 再看: : class overb{
|
z****e 发帖数: 2024 | 3 五体投地。
【在 X****r 的大作中提到】 : Because an "overd *" can be used in place of an "overb *", : but an "overd" cannot be used directly in place of an "overb". : (check Liskov substitution principle) : Think about this: : overd d; : overb b = d; : And this: : overd* pd; : overb* pb = pd; : They are totally different.
|
w****m 发帖数: 146 | 4 upcasting only applies to pointer
【在 z****e 的大作中提到】 : class overb{ : public: : virtual overb f(){}//invalide covariant type : }; : class overd:public overb{ : public: : virtual overd f(){}//invalide covariant type : }; : 再看: : class overb{
|
z****e 发帖数: 2024 | 5 还有reference 啦。
【在 w****m 的大作中提到】 : upcasting only applies to pointer
|