[英]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.