是否可以将 if 语句基于构造函数?
Is it possible to base an if-statement on a constructor?
在我的代码中,我有 3 个不同的构造函数,一个需要两个双打,一个需要 3 个双打,还有一个不需要任何东西。
我还有一个函数可以重载我的 << 以便它可以将它们打印出来,但当时只能打印其中一个。
OnePoint a(3.2, 3.2, 3.2);
OnePoint b(3.1, 3.1);
cout << a << endl;
cout << b << endl;
有什么办法可以这样写:
If(调用带有 2 个参数的构造函数){
(然后这样做)
}else{这样做}
#include <iostream>
using namespace std;
#include <vector>
class OnePoint {
private:
double xvalue;
double yvalue;
double zvalue;
public:
OnePoint(double x, double y) {
xvalue = x;
yvalue = y;
}
OnePoint(double x, double y, double z) {
xvalue = x;
yvalue = y;
zvalue = z;
}
OnePoint() {
}
friend ostream& operator<<(ostream& printh, OnePoint& cPoint) {
if () { //Here
printh << "(" << cPoint.xvalue << ',' << cPoint.yvalue << ")";
}
else {
printh << "(" << cPoint.xvalue << ',' << cPoint.yvalue << "," << cPoint.zvalue << ")";
}
return printh;
}
};
直接录下来怎么样?
#include <vector>
class OnePoint {
private:
double xvalue;
double yvalue;
double zvalue;
bool constructor_with_2_parameters_is_called;
public:
OnePoint(double x, double y) {
xvalue = x;
yvalue = y;
constructor_with_2_parameters_is_called = true;
}
OnePoint(double x, double y, double z) {
xvalue = x;
yvalue = y;
zvalue = z;
constructor_with_2_parameters_is_called = false;
}
OnePoint() {
constructor_with_2_parameters_is_called = false;
}
friend ostream& operator<<(ostream& printh, OnePoint& cPoint) {
if (constructor_with_2_parameters_is_called) {
printh << "(" << cPoint.xvalue << ',' << cPoint.yvalue << ")";
}
else {
printh << "(" << cPoint.xvalue << ',' << cPoint.yvalue << "," << cPoint.zvalue << ")";
}
return printh;
}
};
因为你:
#include <vector>
看来您可能只想使用它:
class OnePoint {
private:
std::vector<double> values;
public:
OnePoint() { }
OnePoint(std::initializer_list<double> v) : values(v) { }
friend std::ostream& operator<<(std::ostream& os, const OnePoint& cPoint) {
os << '(';
bool first = true;
for (double v : cPoint.values) {
if (!first) os << ", ";
os << v;
first = false;
}
return os << ')';
}
};
请注意,您应该通过引用 operator<<
中的 const 来获取 OnePoint
,而不仅仅是通过引用。
这允许:
std::cout << OnePoint{} << std::endl; // prints ()
std::cout << OnePoint{1.0, 2.0} << std::endl; // prints (1, 2)
您的示例反映了一个奇怪的设计选择。就好像你要用同一个class来表示两种点。您当前的设计存在 "What happens if I try to add a 2d point to a 3d point?"
的丑陋问题
这里有两个优雅的答案,要么打印行为不打印 0
value-d Z 坐标(意味着所有点都是 3d,但 2d 点可以伪造),或者使用两个不同的classes。前者允许您将 2d 和 3d 点加在一起,后者则不允许。两者都比你目前允许的情况要好,但会破坏事情。
我建议前者,只需使用检测 0'd z 值的 if 语句。
您的场景可能涉及更复杂的东西,例如多态性,但不太可能。
此外,大多数程序员只会为 2d 和 3d 点打印 (x, y, z)
(我们是一群懒惰的人)。
在我的代码中,我有 3 个不同的构造函数,一个需要两个双打,一个需要 3 个双打,还有一个不需要任何东西。
我还有一个函数可以重载我的 << 以便它可以将它们打印出来,但当时只能打印其中一个。
OnePoint a(3.2, 3.2, 3.2);
OnePoint b(3.1, 3.1);
cout << a << endl;
cout << b << endl;
有什么办法可以这样写:
If(调用带有 2 个参数的构造函数){
(然后这样做)
}else{这样做}
#include <iostream>
using namespace std;
#include <vector>
class OnePoint {
private:
double xvalue;
double yvalue;
double zvalue;
public:
OnePoint(double x, double y) {
xvalue = x;
yvalue = y;
}
OnePoint(double x, double y, double z) {
xvalue = x;
yvalue = y;
zvalue = z;
}
OnePoint() {
}
friend ostream& operator<<(ostream& printh, OnePoint& cPoint) {
if () { //Here
printh << "(" << cPoint.xvalue << ',' << cPoint.yvalue << ")";
}
else {
printh << "(" << cPoint.xvalue << ',' << cPoint.yvalue << "," << cPoint.zvalue << ")";
}
return printh;
}
};
直接录下来怎么样?
#include <vector>
class OnePoint {
private:
double xvalue;
double yvalue;
double zvalue;
bool constructor_with_2_parameters_is_called;
public:
OnePoint(double x, double y) {
xvalue = x;
yvalue = y;
constructor_with_2_parameters_is_called = true;
}
OnePoint(double x, double y, double z) {
xvalue = x;
yvalue = y;
zvalue = z;
constructor_with_2_parameters_is_called = false;
}
OnePoint() {
constructor_with_2_parameters_is_called = false;
}
friend ostream& operator<<(ostream& printh, OnePoint& cPoint) {
if (constructor_with_2_parameters_is_called) {
printh << "(" << cPoint.xvalue << ',' << cPoint.yvalue << ")";
}
else {
printh << "(" << cPoint.xvalue << ',' << cPoint.yvalue << "," << cPoint.zvalue << ")";
}
return printh;
}
};
因为你:
#include <vector>
看来您可能只想使用它:
class OnePoint {
private:
std::vector<double> values;
public:
OnePoint() { }
OnePoint(std::initializer_list<double> v) : values(v) { }
friend std::ostream& operator<<(std::ostream& os, const OnePoint& cPoint) {
os << '(';
bool first = true;
for (double v : cPoint.values) {
if (!first) os << ", ";
os << v;
first = false;
}
return os << ')';
}
};
请注意,您应该通过引用 operator<<
中的 const 来获取 OnePoint
,而不仅仅是通过引用。
这允许:
std::cout << OnePoint{} << std::endl; // prints ()
std::cout << OnePoint{1.0, 2.0} << std::endl; // prints (1, 2)
您的示例反映了一个奇怪的设计选择。就好像你要用同一个class来表示两种点。您当前的设计存在 "What happens if I try to add a 2d point to a 3d point?"
的丑陋问题这里有两个优雅的答案,要么打印行为不打印 0
value-d Z 坐标(意味着所有点都是 3d,但 2d 点可以伪造),或者使用两个不同的classes。前者允许您将 2d 和 3d 点加在一起,后者则不允许。两者都比你目前允许的情况要好,但会破坏事情。
我建议前者,只需使用检测 0'd z 值的 if 语句。
您的场景可能涉及更复杂的东西,例如多态性,但不太可能。
此外,大多数程序员只会为 2d 和 3d 点打印 (x, y, z)
(我们是一群懒惰的人)。