copy-constructor

How could one implement std::auto_ptr's copy constructor?

旧街凉风 提交于 2019-11-26 16:55:59
问题 Back on my crazy AutoArray thingy... (quoting important bits from there: class AutoArray { void * buffer; public: //Creates a new empty AutoArray AutoArray(); //std::auto_ptr copy semantics AutoArray(AutoArray&); //Note it can't be const because the "other" reference //is null'd on copy... AutoArray& operator=(AutoArray); ~AutoArray(); //Nothrow swap // Note: At the moment this method is not thread safe. void Swap(AutoArray&); }; ) Anyway, trying to implement the copy constructor. There's a

When do we have to use copy constructors?

感情迁移 提交于 2019-11-26 14:59:58
I know that C++ compiler creates a copy constructor for a class. In which case do we have to write a user-defined copy constructor? Can you give some examples? sharptooth The copy constructor generated by the compiler does member-wise copying. Sometimes that is not sufficient. For example: class Class { public: Class( const char* str ); ~Class(); private: char* stored; }; Class::Class( const char* str ) { stored = new char[srtlen( str ) + 1 ]; strcpy( stored, str ); } Class::~Class() { delete[] stored; } in this case member-wise copying of stored member will not duplicate the buffer (only the

Uniform initialization fails to copy when object has no data members

回眸只為那壹抹淺笑 提交于 2019-11-26 14:48:53
问题 In updating some code to use uniform initialization, I thought it would be a drop-in modern substitue for the now "old style" parentheses style. I know this isn't always the case (obvious example, vector<int> ) but I've stumbled over another difference that I don't understand. class Object { public: Object() = default; Object(const Object&) = default; }; int main() { Object o; Object copy{o}; // error Object copy2(o); // OK } fails to compile under clang3.5 with the error: (also fails under

C++ template copy constructor on template class

我怕爱的太早我们不能终老 提交于 2019-11-26 14:48:46
问题 I have a template class that has a template copy constructor. The problem is when I instantiate this class using another instance of this class with the same template type, my template copy constructor is not called. Why doesn't it match? Here is the code snippet: #include <iostream> template <typename T> class MyTemplateClass { public: MyTemplateClass() { std::cout << "default constructor" << std::endl; } /* MyTemplateClass(const MyTemplateClass<T>& other) { std::cout << "copy constructor" <

std::thread pass by reference calls copy constructor

冷暖自知 提交于 2019-11-26 14:40:26
Well I have an issue with passing data into a thread using std::thread. I thought I understood the general semantics of copy constructors, etc. but it seems I don't quite grasp the problem. I have a simple class called Log that has hidden it's copy constructor thusly: class Log { public: Log(const char filename[], const bool outputToConsole = false); virtual ~Log(void); //modify behavior void appendStream(std::ostream *); //commit a new message void commitStatus(const std::string str); private: //members std::ofstream fileStream; std::list<std::ostream *> listOfStreams; //disable copy

Can we return objects having a deleted/private copy/move constructor by value from a function?

﹥>﹥吖頭↗ 提交于 2019-11-26 12:29:01
问题 In C++03 it is impossible to return an object of a class having a private non-defined copy constructor by value: struct A { A(int x) { ... } private: A(A const&); }; A f() { return A(10); // error! return 10; // error too! } I was wondering, was this restriction lifted in C++11, making it possible to write functions having a class type return type for classes without constructors used for copy or move? I remember it could be useful to allow callers of a function use the newly returned object,

C++ Compiler Error C2280 “attempting to reference a deleted function” in Visual Studio 2013 and 2015

家住魔仙堡 提交于 2019-11-26 12:28:09
问题 This snippet is compiled without errors in Visual Studio 2013 (Version 12.0.31101.00 Update 4) class A { public: A(){} A(A &&){} }; int main(int, char*) { A a; new A(a); return 0; } while it is compiled with this error in Visual Studio 2015 RC (Version 14.0.22823.1 D14REL): 1>------ Build started: Project: foo, Configuration: Debug Win32 ------ 1> foo.cpp 1>c:\\dev\\foo\\foo.cpp(11): error C2280: \'A::A(const A &)\': attempting to reference a deleted function 1> c:\\dev\\foo\\foo.cpp(6): note

Copy constructor and = operator overload in C++: is a common function possible?

China☆狼群 提交于 2019-11-26 11:31:36
Since a copy constructor MyClass(const MyClass&); and an = operator overload MyClass& operator = (const MyClass&); have pretty much the same code, the same parameter, and only differ on the return, is it possible to have a common function for them both to use? CB Bailey Yes. There are two common options. One - which is generally discouraged - is to call the operator= from the copy constructor explicitly: MyClass(const MyClass& other) { operator=(other); } However, providing a good operator= is a challenge when it comes to dealing with the old state and issues arising from self assignment. Also

Why is copy constructor called instead of conversion constructor?

好久不见. 提交于 2019-11-26 10:35:57
问题 So basically this code: class A { }; class B { B (const B& b) {} public: B (){} B (const A& a) {} }; int main() { A a; B b1(a); //OK B b2 = a; //Error } only generates an error for B b2 = a . And that error is error: ‘B::B(const B&)’ is private Why is it attempting to call the copy constructor in addition to the direct conversion constructor? It\'s clear from the error message that a temporary B is created which is then used for copy-construction, but why? Where is this in the standard? 回答1:

What&#39;s the use of the private copy constructor in c++

你离开我真会死。 提交于 2019-11-26 10:31:33
问题 Why do people define a private copy constructor? When is making the copy constructor and the assignment operator private a good design? If there are no members in the class which are pointers or handles to a unique object (like file name), then wat other cases are there where private copy constructor is a good idea? Same question apply for assignment operator. Given that majority of C++ revolves around copying of objects and passing by reference, are there any good designs which involve