In C++ we often see constructor code like
CircularArc2D::CircularArc2D(const CircularArc2D& arc)
: CircularDisk2D( arc )
{
m_StartPoint = arc.m_StartPoint;
m_EndPoint = arc.m_EndPoint;
}
Run
Here CircularArc2D is a subclass of CircularDisk2D, and the constructor of the
parent class is used for initialization.
Well I don't know too much about C++. But I just wonder how we would do it in
Nim.
type
Point = object of RootObj
x, y: float
# mimic the C++ constructors
proc initPoint(): Point =
Point(x: -1)
# mimic the C++ getters and setters
proc getX(p: Point): float = p.x
proc setX(p: var Point; v: float) =
p.x = v
# maybe another module
type
Circle = object of RootObj
p: Point
r: float
# and now the chained constructors from C++
proc initCircle(): Circle =
result.p = initPoint()
type
Arc = object of Circle
start, `end`: Point
# we want the initialization from initPoint() which is not the binary zero
default!
proc initArc(): Arc =
return Arc(initCircle(Circle(result)))
Run
At least the last line does not compile. But there may be reasons why we do it
this way, maybe Circle object is defined in another module and its fields are
not directly accessible.
I assume this is explained somewhere already, but I can not remember or find
it. And I do not really intent to watch all the youtube videos :-)