`print` function for an object with two different constructo
`print` function for an object with two different constructors
我有一个类Line
,它有两个不同的构造函数:
// constructor - two points
Line::Line(Point& p1l, Point& p2l)
: p1(p1l), p2(p2l) {}
// constructor - point and vector
Line::Line(Point& pl, Vector& dirvec)
: p(pl), v(dirvec) {}
现在我正在尝试实现一个print
函数,但遇到了问题。由于有两个构造函数具有两组不同的参数,我需要提前知道调用了哪个构造函数来创建Line
,并尝试打印。但我不知道如何检查。构造函数的参数是其他类的对象,它们有自己的print
函数。
// print function
void Line::print() const {
cout << "Line[";
p1.print();
cout << ", ";
if (p2) {
p2.print();
}
else if (v) {
v.print();
}
cout << "]";
}
我试图直接检查构造函数的参数是否存在,但它不起作用——我需要初始化print
函数内的对象,这是一个错误的逻辑。
实现这种打印功能的最佳方式是什么?
似乎您的类包含四个字段:第一个构造函数为p1
、p2
,第二个构造函数为带有v
的p
。
这种方法很糟糕,因为每次操作未初始化的字段时都应该消除它们之间的歧义(不仅在print
函数中,而且每次都)。
我建议您选择以下解决方案之一:
保持start
和end
点
你必须改变你的构造函数如下:
Line::Line(const Point& start, const Point& end)
: start_(start), end_(end) {}
// constructor - point and vector
// You should implement operator+ (or a method) for vector and point.
Line::Line(Point& start, Vector& direction)
: start_(start), end_(start + direction) {}
保持start
点和direction
矢量:
// Point - Point should return a Vector. Or you could implement a Vector(Point, Point)
Line::Line(const Point& start, const Point& end)
: start_(start), direction_(end - start) {}
Line::Line(Point& start, Vector& direction)
: start_(start), direction_(direction) {}
您可以在成员变量中保存指示调用了哪个构造函数的信息。
class Line
{
enum class ConstructionInfo : uint8_t
{
NONE = 0,
TWO_POINTS = 1,
POINT_DIRVECTOR = 2
}
ConstructionInfo _ci = ConstructionInfo::NONE;
}
然后在构造函数中初始化这个变量:
// constructor - two points
Line::Line(Point& p1l, Point& p2l)
: p1(p1l), p2(p2l), _ci(ConstructionInfo::TWO_POINTS) {}
// constructor - point and vector
Line::Line(Point& pl, Vector& dirvec)
: p(pl), v(dirvec), _ci(ConstructionInfo::POINT_DIRVECTOR) {}
然后在print()方法中:
switch(_ci)
{
case ConstructionInfo::TWO_POINTS:
// print 2 points
break;
case ConstructionInfo::POINT_DIRVECTOR:
// print a point and a directing vector
break;
}
也许您需要使用枚举类型来指示对象的类型。枚举变量可以在每个构造函数中进行不同的设置。
// constructor - two points
Line::Line(Point& p1l, Point& p2l)
: p1(p1l), p2(p2l), type(POINT_BASED) {}
// constructor - point and vector
Line::Line(Point& pl, Vector& dirvec)
: p(pl), v(dirvec), type(POINT_BASED) {}
// print function
void Line::print() const {
cout << "Line[";
p1.print();
cout << ", ";
if (type == POINT_BASED) {
p2.print();
}
else if (type == VECTOR_BASED) {
v.print();
}
cout << "]";
}
然而,如果它们真的有那么大的不同,也许您还应该考虑将类拆分为从BaseLine派生的两个类,每个类都有自己的打印函数。
相关文章:
- 我遇到了黑客排名中的问题"TWO STRINGS"的三个测试用例的分段错误。原因是什么?
- different between int **arr =new int [ n]; and int a[i][j]?
- Rcpp 'candidate function has different number of parameters (expected 0 but has 1)'
- 'Cannot add two pointers'添加带有 WCHAR 的 LPCWSTR
- 为什么选择 g++ 给予者:"error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]"
- LeetCode #617 "Merge Two Binary Trees"使用C++
- 如何修复c ++中的错误"vector iterators in range are from different containers"?
- C++ UDP RecvFrom, SendTo Different Sockets
- 尝试实施 PIMPL idom 时如何避免"redefinition; different basic types"
- QThread 中的 QTcpSocket 将提交事务,但当 Write 被调用时"Cannot create children for a parent that is in a different
- Typedef redefinition with different types (Emscripten SDL2+O
- C++错误"redefinition of 'string' as different kind of symbol"
- 使用运算符"+" "Merging two files"
- 为什么我会收到这个'redeclared as different kind of symbol'错误?
- "Typedef redefinition with different types" Clang中关于工作MSVC代码的错误
- "two-graph"的最短路径,更改次数有限
- one.cpp和two.cpp都包含两个.h
- C++int via socket send():Linux/MacOS上TCP包中的different值
- 发布模式下的正则表达式提升库链接在使用 mingw-w64 工具链时警告"duplicate section has different size"
- `print` function for an object with two different constructo