为什么说该代码中哪些类型是远不足的?
Tarrayint是Tarray的孩子,为什么我不能在polymorph函数中返回它?
template <class T>
class TArray {
public:
virtual T& operator[](int index) = 0;
virtual void push_back(T num) = 0;
virtual TArray operator+=(T num) = 0;
virtual TArray operator+(T num) = 0;
int size();
void print();
};
class TArrayInt :public TArray<int> {
vector<int> array;
public:
int& operator[](int index);
void push_back(int num);
TArrayInt operator+=(int num); //here is an error
TArrayInt operator+(int num); //and here too
};
TArrayInt is a child of TArray, why I can't return it in polymorph functions?
template <class T>
class TArray {
public:
virtual T& operator[](int index) = 0;
virtual void push_back(T num) = 0;
virtual TArray operator+=(T num) = 0;
virtual TArray operator+(T num) = 0;
int size();
void print();
};
class TArrayInt :public TArray<int> {
vector<int> array;
public:
int& operator[](int index);
void push_back(int num);
TArrayInt operator+=(int num); //here is an error
TArrayInt operator+(int num); //and here too
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
协方差返回类型必须是指示器或参考。您可能希望这些操作员返回参考:
Covariant return types must be pointers or references. You probably want those operators to return references: