Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对于c';任务大纲d和x27;仅包含向量元素的类的tor?_C++_Vector_Constructor_Destructor_Copy Constructor - Fatal编程技术网

C++ 对于c';任务大纲d和x27;仅包含向量元素的类的tor?

C++ 对于c';任务大纲d和x27;仅包含向量元素的类的tor?,c++,vector,constructor,destructor,copy-constructor,C++,Vector,Constructor,Destructor,Copy Constructor,我有一个名为Item的类,它有两个向量作为私有元素 class Item { private: std::vector<std::string> V; std::vector<std::string> E; public: Item(std::vector<std::string> V,std::vector<std::string> E): V(V),E(E){} Item(const Item&a

我有一个名为
Item
的类,它有两个向量作为私有元素

class Item
{
  private:
     std::vector<std::string> V;
     std::vector<std::string> E;
  public:
     Item(std::vector<std::string> V,std::vector<std::string> E): V(V),E(E){}
     Item(const Item& Item)=default;
     ~Item()=default;
      Item& operator=(const Item& Item)=default;

};
类项目
{
私人:
std::向量V;
std::载体E;
公众:
项目(标准::向量V,标准::向量E):V(V),E(E){}
项目(常量项目和项目)=默认值;
~Item()=默认值;
项目和运算符=(常量项目和项目)=默认值;
};
如您所见,我使用了默认的d'tor、copy c'tor和操作符=,
但是这就足够了,还是我应该为它们中的每一个编写主体?

您不需要编写自己的主体,但我会修改构造函数的声明

Item(const std::vector<std::string>& v, const std::vector<std::string>& e):V(v), E(e){}
项(const std::vector&v,const std::vector&e):v(v),e(e){

总是喜欢按常量引用传递大对象,而不是按值传递。按值传递对象可能会导致不必要的对象复制。

否,默认值将执行此操作,请参阅。事实上,你不需要编写任何代码-编译器会综合你需要的东西。我的错,它不能更改。我不应该提到最后一条评论。所以我不必添加
常量
?你最好知道,我是在回答其他人的评论,但后来删除了。我的第一个回答中有一条错误的评论,有人指出了它。感谢他,我现在编辑了答案,他删除了他的评论。如果省略const,这意味着通过引用传递对象,任何创建项实例的人都必须知道,传递给它的向量值在实例化后可能会发生更改。也许他们会在创建实例之前复制它们,直到看到项的构造函数的实现为止。