copy-constructor

Creating a copy constructor for a linked list

耗尽温柔 提交于 2019-11-28 08:15:45
This is homework I'm working on implementing a linked list class for my C++ class, and the copy constructor has be very confusing for me. The linked list is comprised of structs called Elems: struct Elem { int pri; data info; Elem * next; }; Elem * head; info is a separate, custom class that is stored in the Elem. the signature for the copy constructor is: linkedList::linkedList( const linkedList &v ) The issue I am having is mostly taking my logic and actually writing it as code. My general idea is to: Set head to v.head (head = v.head) Set the Elem's values to v's (pri = v.pri , info = v

What is the difference between overloading operator= and overloading the copy constructor?

北城以北 提交于 2019-11-28 07:22:37
问题 What is the difference between overloading the operator = in a class and the copy constructor ? In which context is each one called? I mean, if I have the following: Person *p1 = new Person("Oscar", "Mederos"); Person *p2 = p1; Which one is used? And then when the other one is used? Edit: Just to clarify a little bit: I already know that if we explicitly call the copy constructor Person p1(p2) , the copy constructor will be used. What I wanted to know is when each one is used, but using the =

std::string x(x);

允我心安 提交于 2019-11-28 07:21:44
问题 std::string x(x); This crashes very badly on my compiler. Does this mean I should test for this != &that in my own copy constructors, or can I assume that no client will ever be so stupid? 回答1: Initializing something with itself is undefined behavior, which probably might even mean that once it is invoked you even can't detect it later. Suppose the compiler detects it and out of spite generates assembly for nasal demons, not a call to your copy constructor at all? In practice, you can assume

Are variadic constructors supposed to hide the implicitly generated ones?

一世执手 提交于 2019-11-28 07:18:36
Are variadic constructors supposed to hide the implicitly generated ones, i.e. the default constructor and the copy constructor? struct Foo { template<typename... Args> Foo(Args&&... x) { std::cout << "inside the variadic constructor\n"; } }; int main() { Foo a; Foo b(a); } Somehow I was expecting this to print nothing after reading this answer , but it prints inside the variadic constructor twice on g++ 4.5.0 :( Is this behavior correct? It also happens without variadic templates: struct Foo { Foo() { std::cout << "inside the nullary constructor\n"; } template<typename A> Foo(A&& x) { std:

Why copy constructor is not called here?

一个人想着一个人 提交于 2019-11-28 06:12:13
问题 For the following code : #include<iostream> using namespace std; class Test { public: Test(const Test &t) { cout<<"Copy constructor called"<<endl;} Test() { cout<<"Constructor called"<<endl;} }; Test fun() { cout << "fun() Called\n"; Test t; return t; } int main() { Test t1; Test t2 = fun(); return 0; } I am really confused regarding when the copy constructor is called ? Like if I am running the above program copy constructor is not called. That means if I am messing up with the parameters

Is this good code? (copy constructor and assignment operator )

╄→гoц情女王★ 提交于 2019-11-28 05:33:38
For one reason or another, I'm forced to provide both a copy constructor and an operator= for my class. I thought I didn't need operator= if I defined a copy ctor, but QList wants one. Putting that aside, I hate code duplication, so is there anything wrong with doing it this way? Fixture::Fixture(const Fixture& f) { *this = f; } Fixture& Fixture::operator=(const Fixture& f) { m_shape = f.m_shape; m_friction = f.m_friction; m_restitution = f.m_restitution; m_density = f.m_density; m_isSensor = f.m_isSensor; return *this; } And just out of curiosity, there's no way to switch it so that the bulk

Can I pass a pointer to a superclass, but create a copy of the child?

霸气de小男生 提交于 2019-11-28 05:18:09
问题 I have a function that takes a pointer to a superclass and performs operations on it. However, at some point, the function must make a deep copy of the inputted object. Is there any way I can perform such a copy? It occurred to me to make the function a template function and simply have the user pass the type, but I hold out hope that C++ offers a more elegant solution. 回答1: SpaceCowboy proposes the idiomatic clone method, but overlooked 3 crucial details: class Super { public: virtual Super*

Copy Constructor in C++ is called when object is returned from a function?

送分小仙女□ 提交于 2019-11-28 04:23:36
I understand copy constructor is called on three instances When instantiating one object and initializing it with values from another object. When passing an object by value. 3. When an object is returned from a function by value. I have question with no.3 if copy constructor is called when an object value is returned, shouldn't it create problems if object is declared locally in the function. i mean the copy constructor is a deep copy one and takes reference of an object as parameter It's called exactly to avoid problems. A new object serving as result is initialized from the locally-defined

Copy constructor curly braces initialization

爷,独闯天下 提交于 2019-11-28 03:47:55
问题 "we can initializate objects of a class for which we have not define any constructor using: memberwise initialization. copy initialization. default initialization. For example: struct Work { string author; string name; int year; }; Work s9 { "Bethoven", "Symphony No. 9 in D minor, Op. 125; Choral", 1824 }; // memberwise initialization Work currently_playing {s9}; // copy initialization Work none {}; // default initialization The C++ Programming Language 4th Ed. Chapter 17.3.1 For example:

Copy constructors and Assignment Operators

会有一股神秘感。 提交于 2019-11-28 00:29:24
I wrote the following program to test when the copy constructor is called and when the assignment operator is called: #include class Test { public: Test() : iItem (0) { std::cout << "This is the default ctor" << std::endl; } Test (const Test& t) : iItem (t.iItem) { std::cout << "This is the copy ctor" << std::endl; } ~Test() { std::cout << "This is the dtor" << std::endl; } const Test& operator=(const Test& t) { iItem = t.iItem; std::cout << "This is the assignment operator" << std::endl; return *this; } private: int iItem; }; int main() { { Test t1; Test t2 = t1; } { Test t1; Test t2 (t1); }