C++ 复制构造函数const char*和共享\u ptr
我有一个类C++ 复制构造函数const char*和共享\u ptr,c++,constructor,shared-ptr,C++,Constructor,Shared Ptr,我有一个类关键字: #include <boost/lambda/lambda.hpp> class Keywords { public: ///Constructor Keywords(const char*,vector<shared_ptr<RegularExpression>>,vector<string>); Keywords(const Keywords& other);
关键字:
#include <boost/lambda/lambda.hpp>
class Keywords
{
public:
///Constructor
Keywords(const char*,vector<shared_ptr<RegularExpression>>,vector<string>);
Keywords(const Keywords& other);
private:
const char * xmlFile;
vector<shared_ptr<RegularExpression>> vreg;
vector<string> sw;
}
据我所知,我复制了Const char*和shared_ptr的向量
多谢各位
*所以在删除const char*之后,我将有*
class Keywords
{
public:
///Constructor
Keywords(string,vector<shared_ptr<RegularExpression>>,vector<string>);
Keywords(const Keywords& other);
private:
string xmlFile;
vector<shared_ptr<RegularExpression>> vreg;
vector<string> sw;
}
你有两个选择。您可能希望使用的是编译器提供的默认复制构造函数:
此实现可能仍然无法实现您想要的功能;例如,如果在复制操作之前,我们有rhs.vreg[0]==rhs.vreg[1]
,我们的构造函数将悄悄地复制该单个正则表达式
对象的两个副本,并给我们一个新的关键字
对象*此
,以便此->vreg[0]!=此->vreg[1]
。这只是一般原理的一个具体应用,即很难复制任意指针图
你注意到这行代码有多长了吗
vreg.emplace_back(std::make_shared<RegularExpression>(*sptr));
vreg.emplace_back(std::make_shared(*sptr));
这是普通C++11代码中的一个危险信号。在普通代码中,我们不必编写显式类型,如RegularExpression
;编译器只能推断类型。如果我想复制sptr
,我可以只写vreg.emplace\u back(sptr)
——没有显式类型!但在这里,我必须写出std::make_shared(…)
。这很尴尬。。。因为我做错了。取一个共享\u ptr
,取消引用它,复制构建共享\u ptr
指向的对象,然后将其重新包装到一个新的共享\u ptr
,并具有不同的所有权,这是不正常的。这是一个复杂而奇怪的操作,所以在C++11中执行它的语法是复杂而奇怪的,这是有道理的
在这种情况下,我建议不要耍花招。:) 为什么不使用std::string
而不是char*
?编译器生成的副本成员是正确的。是的,我正在考虑。是的,没错。取而代之的是char const*
到std::string
的隐式转换,因此您可以用同样的方法调用构造函数在智能指针的向量上做得很好!使用shared\u ptr
将允许多个对象共享同一RegularExpression
的所有权,但您不共享所有权,因为您的复制构造函数为新的关键字
对象创建了新的RegularExpression
对象。如果这是你想要的,你写对了。
Keywords::Keywords(const Keywords& other):vreg(other.vreg.size()),sw(other.sw)
{
for (std::size_t i = 0; i < other.vreg.size(); ++i)
vreg[i] = shared_ptr<RegularExpression>(new RegularExpression(*other.vreg[i]));
}
Keywords::~Keywords()
{
sw.clear();
vreg.erase(vreg.begin());
}
class Keywords {
std::string xmlFile;
std::vector<std::shared_ptr<RegularExpression>> vreg;
std::vector<std::string> sw;
public:
Keywords(const char *xmlFile,
const std::vector<std::shared_ptr<RegularExpression>>& vreg,
const std::vector<std::string>& sw)
: xmlFile(xmlFile), vreg(vreg), sw(sw) {}
// Look, Ma, no special member functions!
};
class Keywords {
std::string xmlFile;
std::vector<std::shared_ptr<RegularExpression>> vreg;
std::vector<std::string> sw;
public:
Keywords(const char *xmlFile,
const std::vector<std::shared_ptr<RegularExpression>>& vreg,
const std::vector<std::string>& sw)
: xmlFile(xmlFile), vreg(vreg), sw(sw) {}
Keywords(const Keywords& rhs)
: xmlFile(rhs.xmlFile), sw(rhs.sw)
{
vreg.reserve(rhs.vreg.size());
for (auto&& sptr : rhs.vreg) {
vreg.emplace_back(std::make_shared<RegularExpression>(*sptr));
}
}
// Don't forget to implement copy-assignment too!
// I'm just going to delete it here to keep anyone from using it.
//
Keywords& operator= (const Keywords&) = delete;
};
vreg.emplace_back(std::make_shared<RegularExpression>(*sptr));