【问题标题】:C++ swap the parameter values from same class members, working on multiple classesC++ 交换来自相同类成员的参数值,处理多个类
【发布时间】:2019-03-13 14:44:44
【问题描述】:

所以基本上我有一个 System 类,它构建为一系列平面和球体(它们也是类)。平面和球体(元素)是 Element_CR 类的子类。

例如一个系统看起来像这样 ->

System system0 = {PlaneElement0, Sphere0, Sphere1, PlaneElement1};

现在每个平面和球体都有一个高度参数。 Spheres 和 Planes 有自己的“set”和“get”函数用于高度和其他参数。

Sphere0.set(2.0); // sets the height to 2.0
Sphere0.get();    // returns the height value

我的目标是能够采用 2 个系统 A 和 B(具有相同的平面/球体系列)并交换它们的高度参数。

System systemA = {Sphere0, Sphere1,PlaneElement0};

System systemB = {Sphere2, Sphere3,PlaneElement1};

让我们来看看我的简化代码

class Element_CR {
public:
Element_CR() {};
~Element_CR() {};

virtual Element_CR* crossover_ptr(Element_CR* A, Element_CR* B)=0;
}    

//now the first subclass PlanElement
class PlanElement : public Element_CR
{
public:
PlanElement() {};
PlanElement(double semiHeight) :

    mSemiHeightPlanParam(semiHeight)
{
    buildPlanGeometry_LLT();
};
~PlanElement() {};
//function for setting the height
void set(double height);
//function that returns the height
double get();   
//now the virtual override function 
virtual Element_CR* crossover_ptr(Element_CR* planA, Element_CR* planB) override;


//as I mentioned before the goal is to swap the values between the plane A and plane B Heights.
// So first just getting plane A to have the plane B Height would be fine.

//now the definition of the crossover_ptr function for the Plane element
Element_CR* PlanElement::crossover_ptr(Element_CR* planA, Element_CR* planB) {
PlanElement crossPlan = planA;


//so here i get errors, since when i type "planB." 
//it doesnt show me the "set" function that has been defined in the PlaneElement class
//it says basically "Element_CR* A  expression must have class type"
crossPlan.set(planB.get());

return &crossPlan
}

现在应该对 Sphere 元素(Element_CR 的第二个子类)进行同样的处理,但这可以类推到平面元素。 Sphere Element 类获得相同的“虚拟 Element_CR* crossover_ptr(Element_CR* sphereA, Element_CR* sphereB) 覆盖;”

所以最后我希望能够循环两个系统(由元素构建),并交换元素的高度(两个系统的)。

这可能是一个基本问题,但我是 c++ 新手(可能是大多数发布问题的人)。 我会非常感谢任何建议和帮助, 莱皮娜

【问题讨论】:

  • 您的代码中有多个语法错误,您确定这是真实的代码吗?另外,请解释一下该方法应该做什么,你想交换值吗?返回类型是什么意思?
  • 这是一种伪代码,因为真正的代码太长了。我想混合/交换来自两个相似元素的值(一个元素可以是平面或球体)。例如 System1 由 PlaneElement 和 SphereElement 构建。 System2 也是由 PlaneElement 和 SphereElement 构建的。现在我想交换 System1 PlaneElement 和 System2 PlaneElement 的高度值,然后交换 System1 SphereElement 和 System2 SphereElement 的值。问题是,函数需要识别它是球体还是平面,因此虚拟覆盖函数“crossover_ptr”
  • 另外,如果你能指出语法错误,我可以修复它们或查看原始代码,然后将其复制到这里。
  • Element_CR 的作用是什么?它是平面和球体的概括吗?
  • 是的,没错。因为除了Plane和Sphere,Element类型其实还有很多,这里我只提到了这两个。

标签: c++ overriding virtual


【解决方案1】:
Element_CR* PlanElement::crossover_ptr(Element_CR* planA, Element_CR* planB) {
PlanElement* crossPlanA = dynamic_cast<PlanElement*>(planA);
PlanElement* crossPlanB = dynamic_cast<PlanElement*>(planB);

if(crossPlanA != nullptr && crossPlanB != nullptr) {
    double tmp = crossPlanA->get();
    crossPlanA->set(crossPlanB->get());
    crossPlanB->set(tmp);

    return ... // Whatever you want to return?
}

return nullptr;
}

我认为这符合你想要做的。它首先检查两个元素是否真的是 PlanElements(您可以将其更改为考虑平面和球体),如果是,则交换值。如果它们不是PlanElement 类型,则返回nullptr。你得想想你想回报什么,我想不通。

您可能想重新考虑您的设计,我认为这并不能反映您实际想要做的事情,因为它仅专门用于两个子类,因此您实际上不希望在基类中拥有该方法。

【讨论】:

  • 这对第一个很好,非常感谢!我现在将尝试在整个代码中实现它并尝试一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-02
  • 2022-01-03
  • 1970-01-01
  • 2016-05-09
  • 1970-01-01
  • 2013-06-01
相关资源
最近更新 更多