copy-constructor

Why does the insertion of user defined destructor require an user defined copy constructor

偶尔善良 提交于 2019-12-20 02:44:15
问题 The following code compiles: #include <vector> #include <iostream> #include <memory> using namespace std; class container { public: container(){} ~container(){} }; class Ship { public: Ship(){} //Ship(const Ship & other){cout<<"COPY"<<endl;} //~Ship(){} std::unique_ptr<container> up; }; Ship buildShip() { Ship tmp; return tmp; } int main(int argc, char *argv[]) { return 0; } But if we include the user defined destructor ~Ship(){} , the code will only compile if we also include the user

Calling copy constructor on yourself

青春壹個敷衍的年華 提交于 2019-12-20 02:28:41
问题 I am curious about what is happening in this code I wrote almost by mistake: #include <iostream> class Test { public: Test() { std::cout << "Default constructor"; a= 10; } int a; }; int main() { Test obj(obj); std::cout << obj.a << std::endl; } It compiles in gcc without warnings of any kind (using -Wall -Werror). Executing it only prints garbage. If I am not mistaken, this is calling the implicit copy-constructor on itself, without ever initialising. I am curious about what the copy

Why does the compiler require a copying constructor, need and have moving one and doesn't uses any of them?

不羁的心 提交于 2019-12-19 04:38:36
问题 I've already tried to ask this question but I wasn't clear enough. So here is one more try. And I am very sorry for my English ;) Let's see the code: #include <iostream> #include <memory> using namespace std; struct A { unique_ptr<int> ref; void printRef() { if (ref.get()) cout<<"i="<<*ref<<endl; else cout<<"i=NULL"<<endl; } A(const int i) : ref(new int(i)) { cout<<"Constructor with "; printRef(); } ~A() { cout<<"Destructor with"; printRef(); } }; int main() { A a[2] = { 0, 1 }; return 0; }

Why are iostreams not copyable?

烈酒焚心 提交于 2019-12-19 03:20:15
问题 It's possible to make a local copy of an iostream object, using rdbuf and copyfmt . This allows formatting changes to be locally scoped: std::ostream & operator << ( std::ostream & os, foo const & smth ) { cloned_ostream cs( os ); cs << std::hex << smth.num; // os is not switched to hexadecimal, which would be a confusing side-effect return os; } Why don't the stream classes provide copy constructors to do this? Have relevant C++ best practices changed since they were designed as non-copyable

“CopyConstructible” requirement for C++ stl container element

独自空忆成欢 提交于 2019-12-19 00:55:10
问题 Regarding to the requirement for C++ stl container element, the standard says: the element type should be CopyConstructible, and there is a table for CopyConstructible requirements. Also by various books (Josuttis, etc.), the generated copy should be "equivalent to" the source. I think I need some clarity here. What is exactly "equivalent to"? Also I am a bit confused with the relation between the "CopyConstructible" and the "deep/shallow copy". In general, a copy constructor is either

Can gdb break on implicit class methods?

风流意气都作罢 提交于 2019-12-18 20:54:32
问题 The compiler generates some class methods like copy constructors, destructors, etc. Is it possible to have gdb break on those methods to, e.g., observe where objects are being copied or destroyed? 回答1: Can gdb break on implicit class methods? Yes, of course, it can. (gdb) break MyClass::MyClass(const MyClass &) // break when copied (gdb) break MyClass::~MyClass() // break when object destroyed as simple as that. These are breakpoints based, NOT on file:line, but on function names. If you've a

How to approach copying objects with smart pointers as class attributes?

强颜欢笑 提交于 2019-12-18 16:47:11
问题 From the boost library documentation I read this: Conceptually, smart pointers are seen as owning the object pointed to, and thus responsible for deletion of the object when it is no longer needed. I have a very simple problem: I want to use RAII for pointer attributes of a class that is Copyable and Assignable. The copy and assignment operations should be deep: every object should have its own copy of the actual data. Also, RTTI needs to be available for the attributes (their type may also

How to approach copying objects with smart pointers as class attributes?

老子叫甜甜 提交于 2019-12-18 16:47:10
问题 From the boost library documentation I read this: Conceptually, smart pointers are seen as owning the object pointed to, and thus responsible for deletion of the object when it is no longer needed. I have a very simple problem: I want to use RAII for pointer attributes of a class that is Copyable and Assignable. The copy and assignment operations should be deep: every object should have its own copy of the actual data. Also, RTTI needs to be available for the attributes (their type may also

Strange behavior of copy-initialization, doesn't call the copy-constructor!

落爺英雄遲暮 提交于 2019-12-18 15:51:39
问题 I was reading the difference between direct-initialization and copy-initialization (§8.5/12): T x(a); //direct-initialization T y = a; //copy-initialization What I understand from reading about copy-initialization is that it needs accessible & non-explicit copy-constructor, or else the program wouldn't compile. I verified it by writing the following code: struct A { int i; A(int i) : i(i) { std::cout << " A(int i)" << std::endl; } private: A(const A &a) { std::cout << " A(const A &)" << std:

C++: Could Polymorphic Copy Constructors work?

喜夏-厌秋 提交于 2019-12-18 13:34:14
问题 Consider: class A { public: A( int val ) : m_ValA( val ) {} A( const A& rhs ) {} int m_ValA; }; class B : public A { public: B( int val4A, int val4B ) : A( val4A ), m_ValB( val4B ) {} B( const B& rhs ) : A( rhs ), m_ValB( rhs.m_ValB ) {} int m_ValB; }; int main() { A* b1 = new B( 1, 2 ); A* b2 = new A( *b1 ); // ERROR...but what if it could work? return 0; } Would C++ be broken if "new A( b1 )" was able to resolve to creating a new B copy and returning an A ? Would this even be useful? 回答1: