类固有虚拟字符串未返回正确的东西
Class inherence virtual string not returning the right thing
我有一个奇怪的问题。我举了一个例子来解释问题所在。我有 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
对象并且可以放入数组中。这称为对象切片。
相关文章:
- 为什么使用存储在虚拟方法表中的地址调用虚拟函数的函数会返回垃圾?
- C++如何使虚拟函数返回任何类型的指针
- 返回纯虚拟基类的向量的元素
- 如何模拟C++中使用GMock返回特定值的方法(非虚拟)
- 使用不同的返回类型覆盖虚拟函数
- 返回具体子类实例的 C++ 虚拟类函数
- C++ 使用不同的返回类型覆盖虚拟函数
- 如何拥有两个名称相同但返回类型不同的纯虚拟方法
- 在虚拟方法上返回值
- MSVC:协变返回类型和虚拟继承
- 我可以在 ANTLR4 中使用类似于 c++ 的虚拟令牌(具有相同返回值的令牌)吗?
- 从虚拟方法返回派生对象作为基础
- 我无法让这个虚拟模板化函子运算符返回正确的类型
- getPeername()在客户端在虚拟机上运行时返回我的本地主机地址
- 返回虚拟函数的静态成员,对于抽象类缺少VT
- 提升 Python 包装的虚拟类 - 子类返回错误:与C++签名不匹配
- C 多重继承,虚拟方法覆盖问题和协变量返回类型
- 虚拟方法返回代表派生对象类型的枚举 - 是否可以(在设计方面)
- 如何从虚拟函数返回泛型派生类
- 虚拟函数未返回具有多级继承的派生类型