Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 多态向量元素的具体类型是相同的_C++_Segmentation Fault_Polymorphism_Type Equivalence - Fatal编程技术网

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};