小编t4d*_*ohx的帖子

如何使用继承的operator []来防止赋值?

我有一个自定义结构SortedArrayList<T>,根据比较器对其元素进行排序,我想阻止使用operator[].

例:

ArrayList.h

template <typename T> class ArrayList : public List<T> {
    virtual T& operator[](const int& index) override; //override List<T>
    virtual const T operator[](const int& index) const override; //override List<T>
}
Run Code Online (Sandbox Code Playgroud)

SortedLinkedList.h包含以下运算符

template <typename T> class SortedArrayList : public ArrayList<T> {
   public:

   SortedArrayList<T>(const std::function<bool(const T&, const T&)>& comparator);

   T& operator[](const int& index) override; //get reference (LHS)
   const T operator[](const int& index) const override; //get copy (RHS)
}
Run Code Online (Sandbox Code Playgroud)

Test.h

ArrayList<int>* regular = new ArrayList<int>();
ArrayList<int>* sorted = …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance containers list

0
推荐指数
1
解决办法
99
查看次数

标签 统计

c++ ×1

containers ×1

inheritance ×1

list ×1