copy-constructor

C++ copy constructor syntax: Is ampersand reference to r/l values?

浪尽此生 提交于 2019-12-13 09:04:24
问题 The following is an excerpt from my C++ text, illustrating the syntax for declaring a class with a copy constructor. class Student { int no; char* grade; public: Student(); Student(int, const char*); Student(const Student&); ~Student(); void display() const; }; The copy constructor, as shown here: Student(const Student&); Has an ampersand after the parameter Student. In C, and C++ as-well I believe, the ampersand character is used as a 'address of' operator for pointers. Of course, it is

Writing a valid copy constructor for a hash map in C++

馋奶兔 提交于 2019-12-13 06:56:52
问题 I'm having some trouble creating a copy constructor for my hash map class right now. Currently, I understand how to do a copy constructor for arrays, by copying things over from the original array to the next. For example, here is what would be done for an array list: ArrayList::ArrayList(const ArrayList& a) : items{new std::string[a.cap]}, sz{a.sz}, cap{a.cap} { // arrayCopy is a for loop that does items[i] = a.items[i] on each iteration arrayCopy(items, a.items, sz); } I understand that we

Move semantics and copy constructor

a 夏天 提交于 2019-12-13 04:25:57
问题 I wrote a program as below: #include <iostream> using namespace std; class A { public: A() { } A(A &a) { id = a.id; cout << "copy constructor" << endl; } A& operator=(A &other) { id = other.id; cout << "copy assignment" << endl; return *this; } A(A &&other) { id = other.id; cout << "move constructor" << endl; } A& operator=(A &&other) { id = other.id; cout << "move assignment" << endl; return *this; } public: int id = 10; }; A foo() { A a; return a; } int main() { A a; A a2(a); // output:

Proper Implementation of Copy Constructor and Equals Operator on a class with smart pointers

醉酒当歌 提交于 2019-12-13 02:28:58
问题 Suppose I want to implement a class which is copyable, so I can implement the copy constructor and assignment operator. However, what is the correct implementation and handling of unique and shared pointer variables? See this contrived example which has both types of pointers: Header File #include <memory> using std::unique_ptr; using std::shared_ptr; class Copyable { private: unique_ptr<int> uniquePointer; shared_ptr<int> sharedPointer; public: Copyable(); Copyable(int value); Copyable(const

std::array copy semantics

て烟熏妆下的殇ゞ 提交于 2019-12-13 00:50:07
问题 #include <array> #include <iostream> using namespace std; struct SimpleDebugger { SimpleDebugger(int val = 0) : x(val) { cout << "created" << endl; } SimpleDebugger(const SimpleDebugger &that) : x(that.x) { cout << "copied" << endl; } ~SimpleDebugger() { cout << "killed!" << endl; } int getX() const { return x; } void setX(int val) { x = val; } private: int x; }; array<SimpleDebugger, 3> getInts(int i) { array<SimpleDebugger, 3> a; a[0].setX(i); a[1].setX(i + 1); a[2].setX(i + 2); cout <<

stl vector.push_back() abstract class doesn't compile

断了今生、忘了曾经 提交于 2019-12-12 13:24:03
问题 Let's say I have an stl vector containing class type "xx". xx is abstract. I have run into the issue where the compiler won't let me "instantiate" when i do something like the following: std::vector<xx> victor; void pusher(xx& thing) { victor.push_back(thing); } void main() { ; } I assume this is because the copy constructor must be called. I have gotten around this issue by storing xx*'s in the vector rather than xx's. Is there a better solution? What is it? 回答1: When you use push_back , you

Opening stream via function

北战南征 提交于 2019-12-12 12:40:14
问题 I need help with the non-copyable nature of [io](f)stream s. I need to provide a hackish wrapper around fstream s in order to handle files with unicode characters in their filenames on Windows. For this, I devised a wrapper function: bool open_ifstream( istream &stream, const string &filename ) { #ifdef __GLIBCXX__ FILE* result = _wfopen( convert_to_utf16(filename).c_str(), L"r" ); if( result == 0 ) return false; __gnu_cxx::stdio_filebuf<char>* buffer = new __gnu_cxx::stdio_filebuf<char>(

How to stable_sort without copying?

孤街浪徒 提交于 2019-12-12 11:29:49
问题 Why does stable_sort need a copy constructor? ( swap should suffice, right?) Or rather, how do I stable_sort a range without copying any elements? #include <algorithm> class Person { Person(Person const &); // Disable copying public: Person() : age(0) { } int age; void swap(Person &other) { using std::swap; swap(this->age, other.age); } friend void swap(Person &a, Person &b) { a.swap(b); } bool operator <(Person const &other) const { return this->age < other.age; } }; int main() { static size

Are there any use cases for a class which is copyable but not movable?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 09:32:50
问题 After reading this recent question by @Mehrdad on which classes should be made non-movable and therefore non-copyable , I starting wondering if there are use cases for a class which can be copied but not moved . Technically, this is possible: struct S { S() { } S(S const& s) { } S(S&&) = delete; }; S foo() { S s1; S s2(s1); // OK (copyable) return s1; // ERROR! (non-movable) } Although S has a copy constructor, it obviously does not model the CopyConstructible concept, because that is in turn

Why does resize() cause a copy, rather than a move, of a vector's content when capacity is exceeded? [duplicate]

牧云@^-^@ 提交于 2019-12-12 08:38:03
问题 This question already has answers here : How to enforce move semantics when a vector grows? (3 answers) Closed 6 years ago . Given class X below (special member functions other than the one explicitly defined are not relevant for this experiment): struct X { X() { } X(int) { } X(X const&) { std::cout << "X(X const&)" << std::endl; } X(X&&) { std::cout << "X(X&&)" << std::endl; } }; The following program creates a vector of objects of type X and resizes it so that its capacity is exceeded and