这个派生类是如何抽象的

How is this derived class abstract?

本文关键字:抽象的 派生      更新时间:2023-10-16

所以我有一个抽象基类Collection。我理解它是抽象的,因为它声明了至少一个纯虚拟函数。

我有一个Collection的子类OrderedCollection,它在自己的头文件中声明了这些函数。然后,我在OrderedCollection的Source文件中定义了完全相同的函数。

这是代码:

类别集合.h

class Collection {
public:
virtual Collection& add(int i, int index) = 0;  //pure virtual
virtual Collection& remove(int i) = 0;  //pure virtual
virtual Collection& operator=(const Collection& other)=0;   //pure virtual
virtual int& operator[](int i) = 0; //pure virtual
Collection& iterate(void (*pFunc)());       //Function takes pointer to     function as argument
bool contains(int i);
virtual Collection& copy();

virtual ~Collection();
int size() { return size; }
int capacity() { return capacity; }
//virtual void swap(Collection& other);
protected:
Collection(){}
std::vector collectionVec;
int size;
int capacity;
};

派生类OrderedCollection.h:

class OrderedCollection : public Collection
{
public:
OrderedCollection& add(int i, int index);
OrderedCollection& remove(int i);
OrderedCollection& operator=(const OrderedCollection& other);
int& operator[](int i);
OrderedCollection();
//OrderedCollection::OrderedCollection(int pFirst, int pLast, int pSize, int     pCapacity, std::vector<int> passedVec);
//OrderedCollection(const OrderedCollection& other);    //Copy constructor 
virtual ~OrderedCollection();
virtual OrderedCollection& copy(OrderedCollection& passedCollection);
protected:
//int* first;
//int* last;
int first;
int last;
OrderedCollection& grow();      //Utility Function
};

aaand Ordered Collection.cpp:

#include "OrderedCollection.h"
OrderedCollection& OrderedCollection::add(int i, int index){
if(size == capacity){       //If vector is full
}
return *this;
}
OrderedCollection& OrderedCollection::remove(int i){
if(first <= last){
for(int j = first; j <= last; j++){
if(collectionVec.at(j) == i){
collectionVec.erase(j);
last--;
}
}
}
/*for(int j = 0; j < collectionVec.size(); j++){
if(collectionVec.at(j) == i)
} */
return *this;
}
OrderedCollection& OrderedCollection::operator=(const OrderedCollection& other){

if (this != &other) // protect against invalid self-assignment
{
// 1: allocate new memory and copy the elements
std::vector<int> *new_vector = new std::vector<int>(other.capacity);
std::copy(other.collectionVec, other.collectionVec + other.capacity,     new_vector);
// 2: deallocate old memory
collectionVec.clear();

// 3: assign the new memory to the object
collectionVec = *new_vector;
size = other.size;      //wtf
capacity = other.capacity;      //wtf
delete new_vector;
}
// by convention, always return *this
return *this;

}
int& OrderedCollection::operator[](int i){      //is return type correct? It makes     more sense to have a return type of int or int&, right?
int temp = 0;
if(first <= last){
if(collectionVec.at(first + i) != NULL){    //Need to redo this
return collectionVec.at(first + i);
}
}
return temp;
}
OrderedCollection::OrderedCollection() : Collection()
{
//first = &collectionVec.at(2);
//last = &collectionVec.at(1);
//Crossed at construction
first = 2;
last = 1;
}

OrderedCollection::~OrderedCollection(void)
{
//first = NULL;
//last = NULL;
collectionVec.clear();
}
OrderedCollection& OrderedCollection::grow(){
int newFirst = capacity / 2;
std::vector<int> *new_vector = new std::vector<int>(2 * capacity);
std::copy(collectionVec, collectionVec+size, new_vector->begin() + newFirst);       //Want to return iterator pointing to 
collectionVec = new_vector;
first = newFirst;
last = first + size;
capacity = collectionVec.size;
delete new_vector;
return *this;
}
OrderedCollection& OrderedCollection::copy(OrderedCollection& passedCollection){
OrderedCollection* temp =  new OrderedCollection()   //ERROR is here. VS highlights constructor method
return *this;
}

现在,当我试图在这里的最后一个copy()中创建OrderedCollection类型的值标识符时,问题就来了。根据我的理解,如果类是抽象的,我就不应该被允许这样做(很明显,它是抽象的。VS告诉我也是这样)。但还有另一个问题;当我试图创建一个新的OrderedCollection对象并将其分配给temp时,我也遇到了同样的错误。根据VS,上面的初始化是可以的(IDE没有抱怨,尽管它对我没有任何帮助)。但我不明白为什么它把这个类看作是抽象的。

如果我错了,请纠正我,但这应该涵盖确保这个派生类不是抽象的所有基础。。

  • 派生类中没有声明纯虚拟函数
  • 基类中的所有纯虚拟函数都已在派生类中重写
  • 所有被重写的函数都匹配相关的函数参数签名&返回类型,如最初在基类中声明的

错误确切地说是,错误:不允许使用抽象类类型"OrderedCollection"的对象。。。这是当我尝试在底部的copy()方法中为OrderedCollection的一个新实例分配指针对象时。

让我在下面发布我的Collection.cpp文件:

#include "Collection.h"

Collection::Collection()
{
size = 0;
capacity = 4;
collectionVec.resize(capacity);
}

Collection::~Collection()
{
}
Collection& Collection::iterate(void (*pFunc)()){       
return *this;
}

bool contains(int i){

return true;
}

编辑:添加了Collection.cpp文件,并更新了我对函数参数不匹配的一些修复。

virtual Collection& operator=(const Collection& other)=0;

没有在子类中被重写,因为:

OrderedCollection& operator=(OrderedCollection& other);

具有不同的签名。

区别不仅在于"const",还在于实际类型。重写的类还必须采用Collection,而不是派生的OrderedCollection。