小编Le *_*anh的帖子

如何在C++中创建一个具有多态性的数组?

class Base1
{
    private:
     int testInput; 
    public:
       Base1();
       virtual int GetRow(void) = 0;
 };

 Base1::Base1()
 {
   testInput = 0;
 }

class table : public Base1
{
   private:
    int row;    
   public:  
     table();
     virtual int GetRow(void);
};

table::table()
{   
  //Contructor
  row = 5;
}

int table::GetRow()
{
  return row;
}

int main ()
{
  Base1* pBase = new table[3];
  pBase[0].GetRow();
  pBase[1].GetRow();   //when i get to  this line, the compiler keep saying access
                           // violation.
  pBase[2].GetRow();

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试创建一个3表类的数组.要求是我必须使用Base对象来做到这一点.

Base1 * pBase …
Run Code Online (Sandbox Code Playgroud)

c++ arrays polymorphism virtual inheritance

7
推荐指数
3
解决办法
8835
查看次数

标签 统计

arrays ×1

c++ ×1

inheritance ×1

polymorphism ×1

virtual ×1