C++构造函数写法,以Cura的多边形类Polygons举例
C++构造函数写法,以Cura的类Polygons举例
Cura的类以clipper为基础设计,下面是Polygons的类的源码,可以充分学拷贝构造,重载=等的写法。
Polygon的定义
class Polygon : public PolygonRef
{
ClipperLib::Path poly;
public:
Polygon()
: PolygonRef(poly)
{
}
Polygon(const ConstPolygonRef& other)
: PolygonRef(poly)
, poly(*other.path)
{
}
Polygon(const Polygon& other)
: PolygonRef(poly)
, poly(*other.path)
{
}
Polygon(Polygon&& moved)
: PolygonRef(poly)
, poly(std::move(moved.poly))
{
}
virtual ~Polygon()
{
}
Polygon& operator=(const ConstPolygonRef& other) = delete; // copying a single polygon is generally not what you want
// {
// path = other.path;
// poly = *other.path;
// return *this;
// }
Polygon& operator=(Polygon&& other) //!< move assignment
{
poly = std::move(other.poly);
return *this;
}
};
Polygon
的类,它继承自PolygonRef
。这个类主要用于表示多边形,并提供了构造函数、拷贝构造函数、移动构造函数、析构函数以及赋值运算符重载等成员函数。
- 默认构造函数:创建一个空的多边形对象。
- 拷贝构造函数:根据传入的
ConstPolygonRef
对象创建一个新的多边形对象,复制其内部的poly
成员变量。 - 移动构造函数:根据传入的
Polygon
对象创建一个新的多边形对象,将传入对象的poly
成员变量移动到新对象中。 - 析构函数:释放多边形对象所占用的资源。
- 赋值运算符重载(删除):禁止使用赋值运算符来复制单个多边形对象,因为通常这样做不是预期的行为。
- 赋值运算符重载(移动):根据传入的
Polygon
对象,将其poly
成员变量移动到当前对象中。
Polygons的定义
class Polygons
{
friend class Polygon;
friend class PolygonRef;
friend class ConstPolygonRef;
protected:
ClipperLib::Paths paths;
public:
unsigned int size() const
{
return paths.size();
}
/*!
* Convenience function to check if the polygon has no points.
*
* \return `true` if the polygon has no points, or `false` if it does.
*/
bool empty() const;
unsigned int pointCount() const; //!< Return the amount of points in all polygons
PolygonRef operator[] (unsigned int index)
{
POLY_ASSERT(index < size() && index <= static_cast<unsigned int>(std::numeric_limits<int>::max()));
return paths[index];
}
ConstPolygonRef operator[] (unsigned int index) const
{
POLY_ASSERT(index < size() && index <= static_cast<unsigned int>(std::numeric_limits<int>::max()));
return paths[index];
}
ClipperLib::Paths::iterator begin()
{
return paths.begin();
}
ClipperLib::Paths::const_iterator begin() const
{
return paths.begin();
}
ClipperLib::Paths::iterator end()
{
return paths.end();
}
ClipperLib::Paths::const_iterator end() const
{
return paths.end();
}
/*!
* Remove a polygon from the list and move the last polygon to its place
*
* \warning changes the order of the polygons!
*/
void remove(unsigned int index)
{
POLY_ASSERT(index < size() && index <= static_cast<unsigned int>(std::numeric_limits<int>::max()));
if (index < paths.size() - 1)
{
paths[index] = std::move(paths.back());
}
paths.resize(paths.size() - 1);
}
/*!
* Remove a range of polygons
*/
void erase(ClipperLib::Paths::iterator start, ClipperLib::Paths::iterator end)
{
paths.erase(start, end);
}
void clear()
{
paths.clear();
}
void add(ConstPolygonRef& poly)
{
paths.push_back(*poly.path);
}
void add(const ConstPolygonRef& poly)
{
paths.push_back(*poly.path);
}
void add(Polygon&& other_poly)
{
paths.emplace_back(std::move(*other_poly));
}
void add(const Polygons& other)
{
std::copy(other.paths.begin(), other.paths.end(), std::back_inserter(paths));
}
/*!
* Add a 'polygon' consisting of two points
*/
void addLine(const Point from, const Point to)
{
paths.emplace_back(ClipperLib::Path{from, to});
}
template<typename... Args>
void emplace_back(Args... args)
{
paths.emplace_back(args...);
}
PolygonRef newPoly()
{
paths.emplace_back();
return PolygonRef(paths.back());
}
PolygonRef back()
{
return PolygonRef(paths.back());
}
ConstPolygonRef back() const
{
return ConstPolygonRef(paths.back());
}
Polygons() {}
Polygons(const Polygons& other) { paths = other.paths; }
Polygons(Polygons&& other) { paths = std::move(other.paths); }
Polygons& operator=(const Polygons& other) { paths = other.paths; return *this; }
Polygons& operator=(Polygons&& other) { paths = std::move(other.paths); return *this; }
bool operator==(const Polygons& other) const =delete;
...
Polygons
的类,它用于表示很多多边形的集合。该类具有以下成员函数和成员变量:
friend class Polygon;
:声明了Polygon
类为友元类,允许Polygon
类访问Polygons
类的私有成员。friend class PolygonRef;
:声明了PolygonRef
类为友元类,允许PolygonRef
类访问Polygons
类的私有成员。friend class ConstPolygonRef;
:声明了ConstPolygonRef
类为友元类,允许ConstPolygonRef
类访问Polygons
类的私有成员。protected: ClipperLib::Paths paths;
:定义了一个受保护的成员变量paths
,类型为ClipperLib::Paths
,用于存储多边形的路径信息。public: unsigned int size() const
:返回多边形集合的大小。bool empty() const;
:检查多边形集合是否为空,如果为空则返回true
,否则返回false
。unsigned int pointCount() const;
:返回所有多边形中的点的数量。PolygonRef operator[] (unsigned int index)
:重载下标运算符,返回指定索引处的多边形引用。ConstPolygonRef operator[] (unsigned int index) const
:重载常量下标运算符,返回指定索引处的常量多边形引用。ClipperLib::Paths::iterator begin()
:返回多边形集合的起始迭代器。ClipperLib::Paths::const_iterator begin() const
:返回常量多边形集合的起始迭代器。ClipperLib::Paths::iterator end()
:返回多边形集合的结束迭代器。ClipperLib::Paths::const_iterator end() const
:返回常量多边形集合的结束迭代器。void remove(unsigned int index)
:从列表中移除一个多边形,并将最后一个多边形移动到其位置。void erase(ClipperLib::Paths::iterator start, ClipperLib::Paths::iterator end)
:移除指定范围内的多边形。void clear()
:清空多边形集合。void add(ConstPolygonRef& poly)
:添加一个多边形引用到集合中。void add(const ConstPolygonRef& poly)
:添加一个常量多边形引用到集合中。void add(Polygon&& other_poly)
:添加一个右值引用的多边形到集合中。void add(const Polygons& other)
:将另一个多边形集合添加到当前集合中。void addLine(const Point from, const Point to)
:添加一条由两个点组成的线段作为多边形。template<typename... Args> void emplace_back(Args... args)
:在集合末尾添加一个新的多边形。PolygonRef newPoly()
:创建一个新的多边形并返回其引用。PolygonRef back()
:返回最后一个多边形的引用。ConstPolygonRef back() const
:返回最后一个常量多边形的引用。Polygons() {}
:默认构造函数。Polygons(const Polygons& other) { paths = other.paths; }
:拷贝构造函数,复制另一个多边形集合的路径信息。Polygons(Polygons&& other) { paths = std::move(other.paths); }
:移动构造函数,将另一个多边形集合的路径信息移动到当前对象。Polygons& operator=(const Polygons& other) { paths = other.paths; return *this; }
:赋值运算符重载,将另一个多边形集合的路径信息赋值给当前对象。Polygons& operator=(Polygons&& other) { paths = std::move(other.paths); return *this; }
:移动赋值运算符重载,将另一个多边形集合的路径信息移动赋值给当前对象。bool operator==(const Polygons& other) const =delete;
:删除等于运算符重载,禁止使用等号比较两个多边形集合。
总结
原文地址:https://blog.csdn.net/ULTRAmanTAROACE/article/details/137791597
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!