簡體   English   中英

class 的重載成員 function 能否取決於該 class 的重載構造函數的結果?

[英]Can an overloaded member function of a class depend on the outcome of an overloaded constructor of that class?

我有一個帶有重載構造函數的 class,其中每個版本的構造函數都為該 class 初始化一組不同的私有屬性。 我還有一個 class 的公共成員 function,它將根據 class 的私有屬性執行一些操作。 我想重載成員 function 以便當我從主 function 調用它時,它將執行一個操作並返回一個值。 每個操作將根據相應構造函數的確切結果而有所不同。 這可能嗎? 我如何在 C++ 中實現這個? 這是一些試圖表達這個想法的錯誤代碼:

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

對我來說,這看起來像 inheritance 和虛擬 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. */

我不確定我是否想要你想要的,但從你描述它的方式來看,你可以簡單地根據被調用的構造函數分配一些枚舉。 然后在calcVal()成員 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...
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM