简体   繁体   English

class 的重载成员 function 能否取决于该 class 的重载构造函数的结果?

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

I have a class with an overloaded constructor where each version of the constructor initializes a different set of private attributes for that class.我有一个带有重载构造函数的 class,其中每个版本的构造函数都为该 class 初始化一组不同的私有属性。 I also have a public member function of that class that will perform some operation based on the private attributes of that class.我还有一个 class 的公共成员 function,它将根据 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.我想重载成员 function 以便当我从主 function 调用它时,它将执行一个操作并返回一个值。 Each operation will be different based on the exact outcome of the corresponding constructor.每个操作将根据相应构造函数的确切结果而有所不同。 Is this possible?这可能吗? How could I implement this in C++?我如何在 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
    }
}

For me, this looks like inheritance with a virtual function.对我来说,这看起来像 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. */

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:然后在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.

相关问题 派生类的新构造函数未构建:“未找到重载的成员函数” - Derived class's new constructor not building: “overloaded member function not found” 派生类中的重载构造函数 - Overloaded constructor in derived class 找不到重载的成员函数,该类在标头中 - The overloaded member function is not found, the class is in header 重载了数据成员,构造函数和运算符的抽象基类 - Abstract base class with data member, constructor and operator overloaded 简化重载的类函数 - simplifying overloaded class function 对类成员进行排序时得到错误未解决的重载函数类型 - sort on a class member got the error unresolved overloaded function type 错误C2511:在类中找不到重载的成员函数 - error C2511 : overloaded member function not found in Class 添加指针会导致错误,因为找不到类错误重载的成员函数 - Adding Pointer results in error to class error overloaded member function not found 重载构造函数中的成员初始化 - Member Initialization in overloaded constructor 如果一个类只有一个成员函数通过需求启用它仍然可以模糊地重载? - If a class has only a single member function enabled via requires can it still be ambiguously overloaded?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM