Home > Mobile >  Can an overloaded member function of a class depend on the outcome of an overloaded constructor of t
Can an overloaded member function of a class depend on the outcome of an overloaded constructor of t

Time:11-19

I have a class with an overloaded constructor where each version of the constructor initializes a different set of private attributes for that class. I also have a public member function of that class that will perform some operation based on the private attributes of that class. I want to overload the member function so that when I call it from the main function, it will execute an operation and return a value. Each operation will be different based on the exact outcome of the corresponding constructor. Is this possible? How could I implement this in C ? This is some incorrect code trying to express the idea:

class someClass {
    
    double var1, var2, var3, var4, var5;

    public:
    someClass(double in1) {
        // operations that initialize var1
    }

    someClass(double in1, double in2) {
        // operations that initialize var1 and var2
    }

    someClass(double in1, double in2, double in3) {
        // operations that initialize var1, var2 and var3
    }

    someClass(double in1, double in2, double in3, double in4) {
        // operations that initialize var1, var2, var3 and var4
    }

    someClass(double in1, double in2, double in3, double in4, double in5) {
        // operations that initialize var1, var2, var3, var4 and var5
    }


    double calcVal() {
        return in1   in3;
        // this one is executed if the 1st constructor was called
    }

    double calcVal() {
        return in1   in2;
        // this one is executed if the 2nd constructor was called
    }

    double calcVal() {
        return in1   in2   in3;
        // this one is executed if the 3rd constructor was called
    }

    double calcVal() {
        return in1   in2   in3   in4;
        // this one is executed if the 4th constructor was called
    }

    double calcVal() {
        return in1   in2   in3   in4   in5;
        // this one is executed if the 5th constructor was called
    }
}

CodePudding user response:

For me, this looks like inheritance with a virtual function.

struct someClass {
     virtual ~someClass() {}
     virtual double calcVal() = 0;
};

struct classWithVar1 : someClass {
    double var1;
    classWithVar1(double in1) : var1(in1) {}
    double calcVal() override { return var1; }
};

struct classWithVar2 : someClass {
    double var1, var2;
    classWithVar2(double in1, double in2) : var1(in1), var2(in2) {}
    double calcVal() override { return var1   var2; }
};

/* etc. */

CodePudding user response:

I'm not sure I get want you want, but from how you describe it, you can simply assign some enum depending on the called constructor. And then test it in the calcVal() member function:

class someClass {
    enum class constr { cons1, cons2, cons3, cons4, cons5 };
    double var1, var2, var3, var4, var5;
    constr c;
 
    public:
    someClass(double in1) {
       c = constr::cons1; // and initialisation, of course...
    }

    someClass(double in1, double in2) {
        c = constr::cons2; // more here
    }

    someClass(double in1, double in2, double in3) {
        c = constr::cons3; // more here
    }

    someClass(double in1, double in2, double in3, double in4) {
        c = constr::cons4; // more here
    }

    someClass(double in1, double in2, double in3, double in4, double in5) {
        c = constr::cons5; // more here
    }


    double calcVal() {
        switch(c)
        { 
           case constr::cons1:  return var1   var3;
           case constr::cons2:  return var1   var2;
        // you get the idea...
        }
    }
}
  • Related