C++ 多态向量元素的具体类型是相同的
我得到了一个抽象的基类C++ 多态向量元素的具体类型是相同的,c++,segmentation-fault,polymorphism,type-equivalence,C++,Segmentation Fault,Polymorphism,Type Equivalence,我得到了一个抽象的基类Type,它有多个具体的子类,比如Type\u bool,Type\u int,Type\u double等等。此外,我还构造了两个类型为std::vector的向量来保存具体子类的对象 我实现了以下方法来检查这些向量是否相同。在此上下文中,相同意味着它们具有相同的大小,并且对于向量中的所有元素,它认为type(v1[i])==type(v2[i])对于所有i=1,2。。。v1.尺寸 bool Env::areArgsTheSame(std::vector<Type*
Type
,它有多个具体的子类,比如Type\u bool
,Type\u int
,Type\u double
等等。此外,我还构造了两个类型为std::vector
的向量来保存具体子类的对象
我实现了以下方法来检查这些向量是否相同。在此上下文中,相同意味着它们具有相同的大小,并且对于向量中的所有元素,它认为type(v1[i])==type(v2[i])
对于所有i=1,2。。。v1.尺寸
bool Env::areArgsTheSame(std::vector<Type*> containedFunArgs, std::vector<Type*> otherFunArgs)
{
if(containedFunArgs.size() != otherFunArgs.size())
return false;
bool argsSame = true;
for(std::size_t index = 0; index < containedFunArgs.size(); index++) {
auto *arg = containedFunArgs[index];
auto *otherArg = otherFunArgs[index];
if(typeid(*arg) != typeid(*otherArg)){
argsSame = false;
break;
}
}
return argsSame;
}
考虑一下代码
Type_int *a, *b;
Type_bool* c;
std::vector<Type*> arguments = {a, b};
std::vector<Type*> arguments2 = {a, c};
然后将参数
和参数2
传递到Env::areArgsTheSame
考虑代码
Type_int *a, *b;
Type_bool* c;
std::vector<Type*> arguments = {a, b};
std::vector<Type*> arguments2 = {a, c};
然后将
参数
和参数2
传递到Env::areArgsTheSame
如何创建/初始化包含的FunArgs
和其他FunArgs
,然后再将它们传递到Env::areArgsTheSame
?它们可以包含空指针吗?请提供一个。我已经编辑了问题以包含它。在将它们传递到Env::aregssthesame
之前,如何创建/初始化containedFunArgs
和otherFunArgs
?它们可以包含空指针吗?请提供答案。我已编辑问题以包含它
Type_int a, b;
Type_bool c;
std::vector<Type*> arguments = {&a, &b};
std::vector<Type*> arguments2 = {&a, &c};