copy-constructor

Copy constructor of Array List

情到浓时终转凉″ 提交于 2020-01-30 13:19:09
问题 I just read about deep copy of ArrayList, that people think new ArrayList<>(originalList); will create a shallow copy. And I wrote a small demo ArrayList<String> originalNameList = new ArrayList<>(Arrays.asList("Anna", "Betty", "Chris")); List<String> copyNameList1=originalNameList; List<String> copyNameList2= new ArrayList<>(originalNameList); originalNameList.add("Duke"); copyNameList1.add("Ellen"); copyNameList1.set(1,"Bill"); copyNameList2.set(0,"Andy"); System.out.println(

Copy constructor of Array List

蓝咒 提交于 2020-01-30 13:18:23
问题 I just read about deep copy of ArrayList, that people think new ArrayList<>(originalList); will create a shallow copy. And I wrote a small demo ArrayList<String> originalNameList = new ArrayList<>(Arrays.asList("Anna", "Betty", "Chris")); List<String> copyNameList1=originalNameList; List<String> copyNameList2= new ArrayList<>(originalNameList); originalNameList.add("Duke"); copyNameList1.add("Ellen"); copyNameList1.set(1,"Bill"); copyNameList2.set(0,"Andy"); System.out.println(

Copy constructor of Array List

这一生的挚爱 提交于 2020-01-30 13:17:08
问题 I just read about deep copy of ArrayList, that people think new ArrayList<>(originalList); will create a shallow copy. And I wrote a small demo ArrayList<String> originalNameList = new ArrayList<>(Arrays.asList("Anna", "Betty", "Chris")); List<String> copyNameList1=originalNameList; List<String> copyNameList2= new ArrayList<>(originalNameList); originalNameList.add("Duke"); copyNameList1.add("Ellen"); copyNameList1.set(1,"Bill"); copyNameList2.set(0,"Andy"); System.out.println(

Insert instance of ofstream to container

孤街醉人 提交于 2020-01-30 12:15:45
问题 I created my a class file which is a wrapper for std::ofstream . I created a Container to contain all the instances of file . class file { private: std::ofstream ofs; public: void open(std::string filename); void write(std::string s); void close(); }; class Container { private: std::map<int, file> m; public: void insert(file f,int i) { m.insert(std::pair<int,file> (i,f)); } void get(int i) { m.at(i); } }; However, there is an issue in this code. In the insert method I am attempting to copy a

Insert instance of ofstream to container

假如想象 提交于 2020-01-30 12:15:28
问题 I created my a class file which is a wrapper for std::ofstream . I created a Container to contain all the instances of file . class file { private: std::ofstream ofs; public: void open(std::string filename); void write(std::string s); void close(); }; class Container { private: std::map<int, file> m; public: void insert(file f,int i) { m.insert(std::pair<int,file> (i,f)); } void get(int i) { m.at(i); } }; However, there is an issue in this code. In the insert method I am attempting to copy a

Preventing copy construction and assignment of a return value reference

你说的曾经没有我的故事 提交于 2020-01-24 05:44:06
问题 If I have a function that returns a reference to an instance of a class that I don't have control over its source, say list<int> : list<int>& f(); I want to ensure that its value is only assigned to another reference, e.g.: list<int> &a_list = f(); If the user were instead to do: list<int> a_list = f(); // note: no '&', so the list is copied I want it to be a compile-time error since the user would be manipulating only a copy of the list and not the original list (which is never what is

How delegate from copy constructor to universal copy constructor template?

元气小坏坏 提交于 2020-01-14 03:55:09
问题 If I want to write a universal copy constructor (one that will take any argument type), it's easy enough to do: class Widget { public: template<typename T> Widget(T&& other); }; This won't prevent the compiler from generating a traditional copy constructor, so I'd like to write that myself and delegate to the template. But how do I do that? class Widget { public: template<typename T> Widget(T&& other); Widget(const Widget& other): ??? {} // how delegate to the template? }; I tried to write

Is there a way to disable copy elision in c++ compiler

旧城冷巷雨未停 提交于 2020-01-13 17:58:13
问题 In c++98, the following program is expected to call the copy constructor. #include <iostream> using namespace std; class A { public: A() { cout << "default" ; } A(int i) { cout << "int" ; } A(const A& a) { cout << "copy"; } }; int main () { A a1; A a2(0); A a3 = 0; return 0; } That is evident if you declare the copy constructor explicit in above case (the compiler errors out). But I don't I see the output of copy constructor when it is not declared as explicit. I guess that is because of copy

Is there a way to disable copy elision in c++ compiler

拈花ヽ惹草 提交于 2020-01-13 17:57:18
问题 In c++98, the following program is expected to call the copy constructor. #include <iostream> using namespace std; class A { public: A() { cout << "default" ; } A(int i) { cout << "int" ; } A(const A& a) { cout << "copy"; } }; int main () { A a1; A a2(0); A a3 = 0; return 0; } That is evident if you declare the copy constructor explicit in above case (the compiler errors out). But I don't I see the output of copy constructor when it is not declared as explicit. I guess that is because of copy

Call default copy constructor from within overloaded copy constructor

给你一囗甜甜゛ 提交于 2020-01-13 07:55:45
问题 I need to write a copy constructor that deep copies the contents of a std::shared_ptr . However, there are a bunch of variable int a, b, c, d, e; also defined in the class. Is there a way to generate the default copy constructor code (or call the default copy constructor) inside my new overloaded one. Here is a code snippet with a comment that hopefully clarifies the issue. class Foo { public: Foo() {} Foo(Foo const & other); ... private: int a, b, c, d, e; std::shared_ptr<Bla> p; }; Foo::Foo