Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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++中继承了一些困难。假设我有一个类基父: class Parent{ public: ... virtual Parent Intersection(Parent anotherParent); }_C++_Inheritance - Fatal编程技术网

父对象调用子方法C++; 我在C++中继承了一些困难。假设我有一个类基父: class Parent{ public: ... virtual Parent Intersection(Parent anotherParent); }

父对象调用子方法C++; 我在C++中继承了一些困难。假设我有一个类基父: class Parent{ public: ... virtual Parent Intersection(Parent anotherParent); },c++,inheritance,C++,Inheritance,和2类子数值和符号与方法交叉的实现: class Numeric : public Parent{ public: ... Numeric Intersection(Numeric anotherNumeric) { ... }; // do intersection with another object numeric } // class Symbolic class Symbolic : public Parent{ pu

和2类子数值和符号与方法交叉的实现:

class Numeric : public Parent{
public:
      ...
      Numeric Intersection(Numeric anotherNumeric)
      {
       ...
      }; // do intersection with another object numeric

}

// class Symbolic
class Symbolic : public Parent{
public:
      ...
      symbolic Intersection(Symbolic anotherSymbolic)
      {
       ...
      }; // do intersection with another object symbolic

}
最后一个类ParentVector:

class ParentVector : public Parent{
public:
      ...
      ParentVector Intersection(ParentVector anotherParentVector);

private:
      std::vector<Parent> vtParent; // vector stock object Parent (Numeric or Symbolic)

}
类ParentVector:公共父级{ 公众: ... 父向量交集(父向量另一个父向量); 私人: std::vector vtParent;//矢量对象父对象(数字或符号) } 我想要两种类型的对象:数字或符号。所以我创建了一个父对象的向量

问题是:我想得到两个向量的交集

我可以在vector vtParent中添加一个数字或符号对象,但我不能调用对应每种类型对象的方法。它总是调用类父的方法交集

有人有什么想法或建议吗?非常感谢

//编辑:我忘了ParentVector类也是父类的子类

//更新:谢谢你所有有用的帮助。现在,我想执行下面的代码来计算两个父向量的交点:

ParentVector* Intersection(ParentVector anotherParentVector){
     ParentVector* result;
     Parent* tmp;

     for( int i = 0; i < this->vtParent.size(i); i++ ){
           // PROBLEM with this line because I don't write the code of 
           // function 'virtual Parent* Parent::Intersection(Parent anotherParent)'

          *tmp = this->vtParent.at(i)->Intersection(anotherParentVector.getParentVector().at(i));
          result->getParentVector.push_back(tmp);
     }
}
ParentVector*交集(ParentVector另一个ParentVector){
父向量*结果;
母公司*tmp;
对于(inti=0;ivtParent.size(i);i++){
//这行有问题,因为我没有编写
//函数“虚拟父级*父级::交集(父级另一父级)”
*tmp=this->vtParent.at(i)->交叉点(另一个parentvector.getParentVector().at(i));
结果->getParentVector.push_back(tmp);
}
}
我没有编写函数“virtualparent*Parent::Intersection(Parent-anotherParent)”的代码,因此无法执行上面的代码。有人知道如何解决这个问题吗

//在这里,我想调用函数'Numeric*Intersection(Numeric anotherNumeric)'或'Symbolic*Intersection(Symbolic anotherSymbolic)'


//完成,谢谢您的建议。

您的子类正在更改交集函数的返回类型和参数类型,这本质上使它成为一个新函数,而不是一个行为多态的函数。这些函数必须具有相同的函数签名。

您的子类正在更改
交集
函数的返回类型和参数类型,这本质上使它成为一个新函数,而不是一个行为多态的函数。函数必须具有相同的函数签名。

如果不使用C++11,则当您将符号或数字对象推回向量时,将面临对象切片。在C++11的情况下,如果您的类定义适合,系统将使用move symantic填充向量,您应该定义move Constructor和move assignment运算符,或者取决于这些函数的默认值

要克服对象切片,可以使用

std::vector<Parent*> vtParent;  
请注意,基类中缺少虚拟析构函数

编辑: 您可以简化您的
ParentVector::Intersection()
,如下所示

std::vector<Parent*> Intersection(ParentVector anotherParentVector){
     std::vector<Parent*> result;
     Parent * tmp;
     std::vector<Parent*>::iterator it= vtParent.begin();
     for( ; it != vtParent.end(); ++it){
           tmp=    it->Intersection(anotherParentVector.getParentVector().at(i));
          result->getParentVector.push_back(tmp);
     }
     return result;
}
std::向量交集(父向量另一父向量){
std::向量结果;
母公司*tmp;
std::vector::iterator it=vtParent.begin();
for(;it!=vtParent.end();++it){
tmp=it->Intersection(另一个parentvector.getParentVector().at(i));
结果->getParentVector.push_back(tmp);
}
返回结果;
}

如果不使用C++11,将符号或数字对象推回向量时,将面临对象切片。在C++11的情况下,如果您的类定义适合,系统将使用move symantic填充向量,您应该定义move Constructor和move assignment运算符,或者取决于这些函数的默认值

要克服对象切片,可以使用

std::vector<Parent*> vtParent;  
请注意,基类中缺少虚拟析构函数

编辑: 您可以简化您的
ParentVector::Intersection()
,如下所示

std::vector<Parent*> Intersection(ParentVector anotherParentVector){
     std::vector<Parent*> result;
     Parent * tmp;
     std::vector<Parent*>::iterator it= vtParent.begin();
     for( ; it != vtParent.end(); ++it){
           tmp=    it->Intersection(anotherParentVector.getParentVector().at(i));
          result->getParentVector.push_back(tmp);
     }
     return result;
}
std::向量交集(父向量另一父向量){
std::向量结果;
母公司*tmp;
std::vector::iterator it=vtParent.begin();
for(;it!=vtParent.end();++it){
tmp=it->Intersection(另一个parentvector.getParentVector().at(i));
结果->getParentVector.push_back(tmp);
}
返回结果;
}

是的,这并不要求删除内容,只是为了告诉您需要走的路线

#include <iostream>
#include <vector>
#include <memory>
class Parent{
public:
    virtual Parent* Intersection() = 0;
};

class Numeric : public Parent{
public:
    Parent* Intersection()
    {
        std::cout << "\nNumeric!";
        return new Numeric;
    } // do intersection with another object numeric

};

// class Symbolic
class Symbolic : public Parent{
public:
    Parent* Intersection()
    {
        std::cout << "\nSymbolic!";
        return new Symbolic;
    } // do intersection with another object symbolic

};

class ParentVector{
public:
    ParentVector()
    {
        vtParent.push_back(std::make_unique<Symbolic>());
        vtParent.push_back(std::make_unique<Numeric>());
    }
    void print()
    {
        for (const auto& e : vtParent) {
            e->Intersection();
        }
    }
    ParentVector Intersection(ParentVector anotherParentVector);

private:
    std::vector<std::unique_ptr<Parent>> vtParent; // vector stock object Parent (Numeric or Symbolic)

};


int main()
{
    ParentVector pvec;
    pvec.print();

    std::cout << '\n';
    system("PAUSE");
    return 0;
}
#包括
#包括
#包括
班级家长{
公众:
虚拟父节点*Intersection()=0;
};
类:公共父类{
公众:
父*交集()
{

std::cout是的,这不需要删除东西,只是为了告诉你需要走的路线

#include <iostream>
#include <vector>
#include <memory>
class Parent{
public:
    virtual Parent* Intersection() = 0;
};

class Numeric : public Parent{
public:
    Parent* Intersection()
    {
        std::cout << "\nNumeric!";
        return new Numeric;
    } // do intersection with another object numeric

};

// class Symbolic
class Symbolic : public Parent{
public:
    Parent* Intersection()
    {
        std::cout << "\nSymbolic!";
        return new Symbolic;
    } // do intersection with another object symbolic

};

class ParentVector{
public:
    ParentVector()
    {
        vtParent.push_back(std::make_unique<Symbolic>());
        vtParent.push_back(std::make_unique<Numeric>());
    }
    void print()
    {
        for (const auto& e : vtParent) {
            e->Intersection();
        }
    }
    ParentVector Intersection(ParentVector anotherParentVector);

private:
    std::vector<std::unique_ptr<Parent>> vtParent; // vector stock object Parent (Numeric or Symbolic)

};


int main()
{
    ParentVector pvec;
    pvec.print();

    std::cout << '\n';
    system("PAUSE");
    return 0;
}
#包括
#包括
#包括
班级家长{
公众:
虚拟父节点*Intersection()=0;
};
类:公共父类{
公众:
父*交集()
{
std::cout3个代码问题:

在STL容器中存储值时,将调用复制构造函数,子类对象将被切片。因此,如果要保留某个对象的多态性,则只能使用指针/smart\u指针或引用。在容器场景中,指针/smart\u指针是合适的

std::vector<Parent*> vtParent
代码中的3个问题:

在STL容器中存储值时,将调用复制构造函数,子类对象将被切片。因此,如果要保留某个对象的多态性,则只能使用指针/smart\u指针或引用。在容器场景中,指针/smart\u指针是合适的

std::vector<Parent*> vtParent

您有具有不同返回类型的虚拟函数。请参阅。您有具有不同返回类型的虚拟函数。请参阅。您的意思是,如果我将函数的返回类型:Numeric Intersection(Numeric anotherNumeric)更改为Parent Intersection(Parent anotherNumeric)??您的意思是如果我更改函数的返回类型:Numeric