copy-constructor

What's the most reliable way to prohibit a copy constructor in C++?

ε祈祈猫儿з 提交于 2019-11-26 22:23:36
Sometimes it's necessary to prohibit a copy constructor in a C++ class so that class becomes "non-copyable". Of course, operator= should be prohibited at the same time. So far I've seen two ways to do that. Way 1 is to declare the method private and give it no implementation: class Class { //useful stuff, then private: Class( const Class& ); //not implemented anywhere void operator=( const Class& ); //not implemented anywhere }; Way 2 is to declare the method private and give it "empty" implementation: class Class { //useful stuff, then private: Class( const Class& ) {} void operator=( const

Why user-defined move-constructor disables the implicit copy-constructor?

╄→гoц情女王★ 提交于 2019-11-26 21:54:17
While I'm reading boost/shared_ptr.hpp, i saw this code: // generated copy constructor, destructor are fine... #if defined( BOOST_HAS_RVALUE_REFS ) // ... except in C++0x, move disables the implicit copy shared_ptr( shared_ptr const & r ): px( r.px ), pn( r.pn ) // never throws { } #endif What does the comment "generated copy constructor, destructor are fine except in C++11, move disables the implicit copy" mean here? Shall we always write the copy ctor ourselves to prevent this situation in C++11? I've upvoted ildjarn's answer because I found it both accurate and humorous. :-) I'm providing

copy constructor of derived QT class

那年仲夏 提交于 2019-11-26 21:03:34
问题 I have a class which is publicly inherited from QWidget : class MyWidget : public QWidget { Q_OBJECT public: MyWidget(const MyWidget& other) : obj1(other.obj1), obj2(other.obj2) private: some_class obj1; some_class obj2; }; When I built my project, compiler complains: WARNING:: Base class "class QWidget" should be explicitly initialized in the copy constructor. I checked out from other questions on stackoverflow, and got my answer. But the fact is, when I added that initialization like this:

why copy constructor is called when passing temporary by const reference?

坚强是说给别人听的谎言 提交于 2019-11-26 20:25:36
问题 I am passing an unnamed temporary object to a function defined with const ref parameter. The copy ctor of the class is private, and I get a compilation error. I don't understand why a copy constructor is called in this situation. class A { public: A(int i) {} private: A(const A&) {} }; void f(const A& a) { } int main() { f(A(1)); // <-- error here: 'A::A(const A&)' is private } As expected, when I change the main to: A a(1); f(a); it works. EDIT: the compiler is gcc 4.1.2 回答1: You can find

The copy constructor and assignment operator

*爱你&永不变心* 提交于 2019-11-26 19:51:02
If I override operator= will the copy constructor automatically use the new operator? Similarly, if I define a copy constructor, will operator= automatically 'inherit' the behavior from the copy constructor? sgokhales No, they are different operators. The copy constructor is for creating a new object. It copies a existing object to a newly constructed object.The copy constructor is used to initialize a new instance from an old instance. It is not necessarily called when passing variables by value into functions or as return values out of functions. The assignment operator is to deal with an

C++: Deep copying a Base class pointer

老子叫甜甜 提交于 2019-11-26 19:30:40
问题 I searched around and seems in order to perform this I need to change my Base class and want to know if this is the best approach. For example, I have a Base class: class Base {} Then a long line of derived classes: class Derived_1:: public Base {} class Derived_2:: public Derived_1{} ... ... class Derived_n:: public Derived_M{} And then I have another class: class DeepCopy { Base * basePtr; public: DeepCopy(DeepCopy & dc) {} } Assuming the Base class and Derived_x class copy constructors are

Why is this code trying to call the copy constructor?

江枫思渺然 提交于 2019-11-26 19:06:20
I just spent an inordinate amount of time fiddling with a complilation error in Visual Studio. I have distilled the code into the small compilable example below and tried it on IdeOne and got the same error which you can see here . I am wondering why the following code tries to call B(const B&) instead of B(B&&) : #include <iostream> using namespace std; class A { public: A() : data(53) { } A(A&& dying) : data(dying.data) { dying.data = 0; } int data; private: // not implemented, this is a noncopyable class A(const A&); A& operator=(const A&); }; class B : public A { }; int main() { B binst;

C++ Copy constructor gets called instead of initializer_list<>

别等时光非礼了梦想. 提交于 2019-11-26 18:25:36
问题 Based on this code struct Foo { Foo() { cout << "default ctor" << endl; } Foo(std::initializer_list<Foo> ilist) { cout << "initializer list" << endl; } Foo(const Foo& copy) { cout << "copy ctor" << endl; } }; int main() { Foo a; Foo b(a); // This calls the copy constructor again! //Shouldn't this call the initializer_list constructor? Foo c{b}; _getch(); return 0; } The output is: default ctor copy ctor copy ctor In the third case, I'm putting b into the brace-initialization which should call

Copy constructor vs. return value optimization

一世执手 提交于 2019-11-26 17:49:16
问题 In a previous question, it appeared that a plain return-by-value function always copies its return argument into the variable being assigned from it. Is this required by the standard, or can the function be optimized by constructing the 'assigned to' variable even within the function body? struct C { int i; double d; }; C f( int i, int d ) { return C(i,d); // construct _and_ copy-construct? } int main() { C c = f( 1, 2 ); } 回答1: The standard allows any level of copy omission here: construct a

Copy constructor elision? [duplicate]

半腔热情 提交于 2019-11-26 16:56:45
问题 Possible Duplicate: Why has the destructor been called only once? Given the code below, I fail to understand the output in gcc. I expect two objects to be created and destroyed but instead see only one call to the constructor and the destructor. What's happening here? #include <string> #include <iostream> struct Huge{ Huge() { std::cout << "Constructor" << std::endl; } Huge(Huge const &r) { std::cout << "Copy Constructor" << std::endl; } ~Huge() { std::cout << "Destructor" << std::endl; } };