类固有虚拟字符串未返回正确的东西

Class inherence virtual string not returning the right thing

本文关键字:返回 虚拟 字符串      更新时间:2023-10-16

我有一个奇怪的问题。我举了一个例子来解释问题所在。我有 4 个类,其中一个类指向其他 2 个类固有的类的指针。这是它的样子:固有类:

class classA{
public:
  classA(){}
  virtual std::string getType(){return "classA";}
  classA& operator=(const classA& classa) {return *this;}
};
class classB: public classA {
  int b;
public:
  classB(int n){b=n;}
  virtual std::string getType() { return "classB"; }
  void setB(const int b){this->b=b;}
  int getB() const{return this->b;}
};
class classC: public classA {
  int c;
public:
  classC(int n){c=n;}
  virtual std::string getType() { return "classC"; }
  void setC(const int c){this->c=c;}
  int getC() const{return this->c;}
};

唯一重要的是getType()功能。

现在是获取指向类 A 的指针的类

class superClass{
  classA* _classA;
  int nb;
public:
  superClass(){nb=0;}
  void addElement(classA& e){
    classA *newTab=new classA[++nb]; // create tab as the same size than the other +1
    for(int i=0;i<nb-1;i++)
      newTab[i]=_classA[i]; // add element from the old class to the new one
    newTab[nb-1]=e; // add the element
    //delete[] _classA; 
    _classA=newTab; // now copy it to the class
    //delete[] newTab;
  }
  classA* getClass() {return _classA;}
  int getNb() const{return this->nb;}
  void displayElements(){
    for(int i=0;i<getNb();i++)
        std::cout << _classA[i].getType() << std::endl;
  }
};

addElemment() 是一个函数,它用一个空格来 malloc 一个 classA 元素,它充满了古老的元素,然后它添加了新元素,然后就在这里。是有效的,但问题就在这里。我不使用 classA 元素,只使用它的子元素。我想在超类中添加 B 类元素和类 C 元素,并使用 getType() 获取类类型;这是主文件

int main(int argc, char const *argv[])
{
  classB *classb = new classB(9);
  classC *classc = new classC(10);
  superClass super;
  super.addElement(*classb);
  super.displayElements();
  // Display "classA" instead of "classB"
  super.addElement(*classc);
  super.displayElements();
  // Display "classA" and "classA" instead "classB" and "classC"
  //std::cout << classb->getType() << std::endl; // return ClassA
  //std::cout << classc->getType() << std::endl; // return ClassA
  return 0;
}

我只想让我的程序显示正确的类,孩子一个类。我认为问题出在addElement()上。我尝试使用虚拟std::string getType()=0;但它仍然不起作用,它没有任何改变。

我也尝试使用模板,但什么也没改变,也不起作用

我的问题:我希望我的程序每次都显示子类而不是类A。

您应该将超类中的声明成员_classA更改为以下内容:classA** _classA;。所以它会是这样的:

class superClass
{
   classA** _classA;
   int nb;
public:
   superClass():_classA(0) // you also should initialize this to avoid crash while first delete[] of this _classA
   {
      nb = 0;
   }
   ~superClass() // also you should add destructor to free memory
   {
      for (int i = 0; i < nb; i++)
      {
          delete _classA[i];
          _classA[i] = nullptr;
      }
      delete[] _classA;
      _classA[i] = nullptr;
   }
   void addElement(classA& e)
   {
      int oldSize = nb;
      nb++; // increment the size separately for clarity
      classA **newTab = new classA*[nb]; // create tab as the same size than the other +1
      for (int i = 0; i < oldSize; i++)
         newTab[i] = _classA[i]; // add element from the old class to the new one
      classA* newElement = new classA(e); // use the copy-constructor
      newTab[oldSize] = newElement; // add the element
      delete[] _classA; // now you can free it
      _classA = newTab; // now copy it to the class
   }
   classA** getClass()
   {
      return _classA;
   }
   int getNb() const
   {
      return this->nb;
   }
   void displayElements()
   {
      for (int i = 0; i < getNb(); i++)
         std::cout << _classA[i]->getType() << std::endl;
   }
};

>newTab是一个classA数组。因此,它不能包含classB对象,只能包含classA对象。

newTab[nb-1]=e;

在这里,如果e引用一个classB对象,则此赋值会将classB部分从它身上切开,因此它成为一个classA对象并且可以放入数组中。这称为对象切片。