Search code examples
c++overridingvirtual

C++ swap the parameter values from same class members, working on multiple classes


so basically I have a System class that is built as a series of planes and spheres (which are also classes). The planes and spheres (the elements) are subclasses of the class Element_CR.

For instance a System looks like this ->

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

Now every Plane and Sphere got a height parameter. The Spheres and Planes got own "set" and "get" function for the heights and other parameters.

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

My goal is to be able to take 2 Systems A and B(with the same Plane/Sphere series) and swap their Height parameters.

System systemA = {Sphere0, Sphere1,PlaneElement0};

System systemB = {Sphere2, Sphere3,PlaneElement1};

So lets get to my simplified code

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
}

Now the same should be done with the Sphere Element (the second subclass of Element_CR), but that can be analogically solved to the Plane Element. Sphere Element class gets the same "virtual Element_CR* crossover_ptr(Element_CR* sphereA, Element_CR* sphereB) override;"

So at the end I want to be able to loop two Systems(built of Elements), and swap the heights of the Elements (of the two systems).

It is probably a basic problem but Im new to c++(probably as most people that are posting questions). I would be really thankful for any suggestions and help, lepina


Solution

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

    I think this matches what you want to do. It first checks if both Elements are truly PlanElements (you can change this to consider Planes and Spheres) and if so, it swaps the values. It returns nullptr if they are not of the type PlanElement. You have to think about what you want to return, I couldn't figure out a sense there.

    You may want to reconsider your design, I don't think this reflects what you actually want to do since it is only specifically for two subclasses and hence you actually don't want to have the method in the base class.