【问题标题】:Abstract Class and Pure Virtual Method in C++C++中的抽象类和纯虚方法
【发布时间】:2012-10-01 15:04:50
【问题描述】:

我有 4 个 C++ 文件、2 个标头和 2 个 .cc 文件。这只是一个概念证明,但我似乎无法正确理解。

我的第一个标题如下所示:

#ifndef INT_LIST_H
#define INT_LIST_H
class IntList
{
  public:

     //Adds item to the end of the list
     virtual void pushBack(int item) = 0;
};
#endif

我的第二个标题使用第一个,看起来像这样:

#ifndef ArrayIntList_H
#define ArrayIntList_H
#include "IntList.h"


class ArrayIntList : public IntList
{
    private:
        int* arrayList;
        int* arrayLength;

     public:
        //Initializes the list with the given capacity and length 0
        ArrayIntList(int capacity);

        //Adds item to the end of the list
        virtual void pushBack(int item) = 0;

};
#endif  

我的第一个.cc文件里面填的是上一个类的方法:

#include <iostream>
#include "ArrayIntList.h"

ArrayIntList::ArrayIntList(int capacity)
{
    //make an array on the heap with size capacity
    arrayList = new int[capacity];
    //and length 0
    arrayLength = 0; 
}

void ArrayIntList::pushBack(int item)
{
    arrayList[*arrayLength] = item;
}

这是我的主要功能:

#include <iostream>
#include "ArrayIntList.h"

int main(int argc, const char * argv[])
{
    ArrayIntList s(5);
}

当我在 Xcode 中运行它时,我收到“变量 ArrayIntList 是一个抽象类”的错误 我不明白这是怎么回事,因为我在上面的 .cc 文件中定义了它。有什么想法吗?

【问题讨论】:

    标签: c++ abstract-class


    【解决方案1】:

    在类 ArrayIntList 上使用这个

    virtual void pushBack(int item);
    

    而不是这个

    virtual void pushBack(int item) = 0;
    

    原因是当你将 0 分配给函数声明时,你说它是“纯的”,或者没有实现。但是你在下面这样做(实现它)。

    【讨论】:

    • 宾果游戏,你是救生员。你能解释一下为什么吗?
    • 你打败了我,完美。谢谢!
    • 纯方法只是继承类中实现的“承诺”。将 0 分配给函数声明就是将其声明为“纯”。如果你尝试用纯方法实例化一个类,你会得到一个编译器错误。
    • @Tom W 是对的,您还应该将 arrayLength 声明为整数,而不是指向整数的指针。
    【解决方案2】:

    您已将ArrayIntList::pushBack(int item) 声明为纯虚函数。这就是= 0 所做的。从 ArrayIntList.h 中删除 = 0。

    另外:您正在使用 int 指针而不是 int 来跟踪数组长度。

    【讨论】:

      【解决方案3】:

      在 ArrayIntList 类的声明中,您需要从方法声明中删除“= 0”。您可能还需要将 arrayLength 声明为 int 而不是指向 int 的指针。最后,由于您在构造函数中为数组分配内存,因此您应该声明一个析构函数以在对象被销毁时释放内存:

      class ArrayIntList : public IntList
      {
      private:
          int* arrayList;
          int arrayLength;
      
      public:
          //Initializes the list with the given capacity and length 0
          ArrayIntList(int capacity);
      
          virtual ~ArrayIntList() { delete arrayList; }
      
          //Adds item to the end of the list
          virtual void pushBack(int item);
      
      };
      

      当然,处理数组列表的最佳方式是使用std::vector&lt;int&gt;,这样您就不必手动处理内存分配和释放

      【讨论】:

        【解决方案4】:

        在类 ArrayIntList 中,您声明了一个纯虚拟“virtual void pushBack(int item) = 0;”您已经在抽象父 IntList 中声明了它。您需要做的就是将其声明为“virtual void pushBack(int item);”。

        【讨论】:

          【解决方案5】:

          一个抽象基类不能继承另一个抽象基类,去掉

          = 0;
          

          来自您在 ArrayIntList 中的方程式:

          virtual void pushBack(int item) = 0;
          

          【讨论】:

            猜你喜欢
            • 2015-08-14
            • 2019-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-07-13
            • 2016-01-25
            • 1970-01-01
            • 1970-01-01
            • 2013-04-09
            相关资源
            最近更新 更多