Hum*_*awi 1 c++ overloading copy-constructor c++11
对于我没写过的类,是否可以添加一个复制构造函数(换句话说就是重载它)?
例如,我正在使用一些具有Point类的库.我想为它添加一个拷贝构造函数.我不能也不想编辑它.
想象的语法:
cv::Point cv::Point::Point(const AnotherPoint& p){
return cv::Point(p.x,p.y);
}
Run Code Online (Sandbox Code Playgroud)
PS我也没写AnotherPoint.
EDIT -Problem背景 - :
我想用标准函数复制std::vector<cv::Point>到另一个问题的所有问题.所以我正在寻找一种方法来重载复制构造函数来实现它.std::vector<AnotherPoint>std::copy
在定义之后,您无法将构造函数添加到类型中.
将a复制std::vector<cv::Point>到a的简单方法std::vector<AnotherPoint>是使用std::transform:
std::vector<cv::Point> cvPoints;
//cvPoints filled
std::vector<AnotherPoint> otherPoints;
otherPoints.reserve(cvPoints.size()); //avoid unnecessary allocations
std::transform(std::begin(cvPoints), std::end(cvPoints),
std::back_inserter(otherPoints),
[](const cv::Point& p){ return AnotherPoint{p.x, p.y}; });
Run Code Online (Sandbox Code Playgroud)