copy-constructor

Why is copy constructor not being called in this code

自古美人都是妖i 提交于 2019-11-27 16:31:10
So why is Copy constructor not being invoked in " const Integer operator+(const Integer &rv) " function. Is it because of RVO. If Yes what do I need to do to prevent it? #include <iostream> using namespace std; class Integer { int i; public: Integer(int ii = 0) : i(ii) { cout << "Integer()" << endl; } Integer(const Integer &I) { cout << "Integer(const Integer &)" << endl; } ~Integer() { cout << "~Integer()" << endl; } const Integer operator+(const Integer &rv) const { cout << "operator+" << endl; Integer I(i + rv.i); I.print(); return I; } Integer &operator+=(const Integer &rv) { cout <<

C++ copy-construct construct-and-assign question

扶醉桌前 提交于 2019-11-27 15:38:58
Here is an extract from item 56 of the book "C++ Gotchas": It's not uncommon to see a simple initialization of a Y object written any of three different ways, as if they were equivalent. Y a( 1066 ); Y b = Y(1066); Y c = 1066; In point of fact, all three of these initializations will probably result in the same object code being generated, but they're not equivalent. The initialization of a is known as a direct initialization, and it does precisely what one might expect. The initialization is accomplished through a direct invocation of Y::Y(int). The initializations of b and c are more complex

Copy constructor elision? [duplicate]

人走茶凉 提交于 2019-11-27 14:52:32
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; } }; Huge g() { std::cout << "Entering g" << std::endl; Huge temp; std::cout << "Exiting g" << std::endl;

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

这一生的挚爱 提交于 2019-11-27 14:51:04
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 piece of client code (not yet committed into bitbucket because it won't build) that looks like this:

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

时光总嘲笑我的痴心妄想 提交于 2019-11-27 14:39:25
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 the initializer_list<> constructor. Instead, the copy constructor takes the lead. Will someone of you

What is copy/move constructor choosing rule in C++? When does move-to-copy fallback happen?

岁酱吖の 提交于 2019-11-27 13:50:54
问题 The first example: #include <iostream> #include <memory> using namespace std; struct A { unique_ptr<int> ref; A(const A&) = delete; A(A&&) = default; A(const int i) : ref(new int(i)) { } ~A() = default; }; int main() { A a[2] = { 0, 1 }; return 0; } It works perfectly. So here the MOVE constructor is used. Let's remove the move constructor and add a copy one: #include <iostream> #include <memory> using namespace std; struct A { unique_ptr<int> ref; A(const A&a) : ref( a.ref.get() ? new int(*a

Copy constructor with pointers

本秂侑毒 提交于 2019-11-27 13:04:16
问题 I have recently discovered that when I have pointers within a class, I need to specify a Copy constructor. To learn that, I have made the following simple code. It compiles, but gives me runtime error when performing the copy constructor. I am trying to copy just the value from the pointer of the copied object, but avoiding assigning the same address. So, what's wrong here? class TRY{ public: TRY(); ~TRY(); TRY(TRY const &); int *pointer; void setPointer(int); }; void TRY::setPointer(int a){

default copy constructor

﹥>﹥吖頭↗ 提交于 2019-11-27 10:47:43
问题 Can the (implicit) default copy constructor be called for a class that has already user-defined constructor but that is not the copy constructor ? If it is possible then, suppose we define the copy constructor for the class explicitly , now can the (implicit)default constructor be called? 回答1: First, let's clarify our vocabulary a bit. A default constructor is a constructor which can be called without any arguments. A copy constructor is a constructor which can be called with a single

C++ template copy constructor on template class

懵懂的女人 提交于 2019-11-27 09:41:47
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::endl; } */ template <typename U> MyTemplateClass(const MyTemplateClass<U>& other) { std::cout <<

Copy constructor vs. return value optimization

怎甘沉沦 提交于 2019-11-27 09:18:54
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 ); } The standard allows any level of copy omission here: construct a local temporary, copy-construct it into a return value, and copy-construct the return value into the local