class 的重载成员函数能否依赖于 class 的重载构造函数的结果?
Can an overloaded member function of a class depend on the outcome of an overloaded constructor of that class?
我有一个带有重载构造函数的 class,其中构造函数的每个版本都为 class 初始化一组不同的私有属性。我还有一个 class 的 public 成员函数,它将根据 class 的私有属性执行一些操作。我想重载成员函数,以便当我从主函数调用它时,它将执行一个操作和 return 一个值。根据相应构造函数的确切结果,每个操作都会有所不同。这可能吗?我如何在 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
}
}
我不确定我是否得到你想要的,但根据你的描述,你可以根据调用的构造函数简单地分配一些枚举。然后在calcVal()
成员函数中测试:
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...
}
}
}
对我来说,这看起来像是带有虚函数的继承。
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. */
我有一个带有重载构造函数的 class,其中构造函数的每个版本都为 class 初始化一组不同的私有属性。我还有一个 class 的 public 成员函数,它将根据 class 的私有属性执行一些操作。我想重载成员函数,以便当我从主函数调用它时,它将执行一个操作和 return 一个值。根据相应构造函数的确切结果,每个操作都会有所不同。这可能吗?我如何在 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
}
}
我不确定我是否得到你想要的,但根据你的描述,你可以根据调用的构造函数简单地分配一些枚举。然后在calcVal()
成员函数中测试:
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...
}
}
}
对我来说,这看起来像是带有虚函数的继承。
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. */