copy-constructor

What's all the fuss about C++ copy constructors? [duplicate]

我是研究僧i 提交于 2019-12-03 07:12:22
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: When do we have to use copy constructors? Why exactly are C++ copy constructors so important? I just learned about them and I don't quite see what is the fuss about them. It seems you should always write a copy constructor for your classes if you use pointers, but why? Thanks, Boda Cydo. 回答1: Copy constructors and assignment operators are very important in C++ because the language has "copy semantics", that is

assignment operator vs. copy constructor C++

99封情书 提交于 2019-12-03 04:56:01
问题 I have the following code to test out my understanding of basic pointers in C++: // Integer.cpp #include "Integer.h" Integer::Integer() { value = new int; *value = 0; } Integer::Integer( int intVal ) { value = new int; *value = intVal; } Integer::~Integer() { delete value; } Integer::Integer(const Integer &rhInt) { value = new int; *value = *rhInt.value; } int Integer::getInteger() const { return *value; } void Integer::setInteger( int newInteger ) { *value = newInteger; } Integer& Integer:

Why aren't copy constructors “chained” like default constructors or destructors?

 ̄綄美尐妖づ 提交于 2019-12-03 04:22:39
问题 This might be a question with an obvious answer or a duplicate. If so, sorry, I'll delete it. Why aren't copy constructors chained (like default ctors or dtors) so that before the derived class's copy constructor is called, the base class's copy constructor is called? With copy constructors and destructors, they are called in a chain from base-to-derived and derived-to-base, respectively. Why isn't this the case for copy constructors? For example, this code: class Base { public: Base() :

C++ copy constructor using pointers

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 03:07:42
Can anyone explain the meaning of *p=*q in this C++ code? Is this a copy constructor concept? class A{ //any code } int main(){ A *p=new A(); A *q=new A(); *p=*q; return 0; } Is this a copy constructor concept? No, what you are referring to is a copy assignment concept. Consider this: int* p = new int{9}; int* q = new int{10}; *p = *q; As you can see above, only the value of the variable q which is pointed to is copied. This is the equivalent of the copy assignment for objects. If you were to do this: p = q; Then this would not be a copy assignment, because both int 's point to the same

Copy constructor not called when initializing an object with return value of a function

半世苍凉 提交于 2019-12-03 03:03:29
Consider the following code: #include <iostream> using namespace std; class A { public: int a; A(): a(5) { cout << "Constructor\n"; } A(const A &b) { a = b.a; cout << "Copy Constructor\n"; } A fun(A a) { return a; } }; int main() { A a, c; A b = a.fun(c); return 0; } The output of the above code with g++ file.cpp is: Constructor Constructor Copy Constructor Copy Constructor The output of the above code with g++ -fno-elide-constructors file.cpp is: Constructor Constructor Copy Constructor Copy Constructor Copy Constructor I know Return Value Optimization. My question is which call to copy

Copy constructor initialization lists

ε祈祈猫儿з 提交于 2019-12-03 02:27:55
问题 I know that if you leave a member out of an initialization list in a no-arg constructor, the default constructor of that member will be called. Do copy constructors likewise call the copy constructor of the members, or do they also call the default constructor? class myClass { private: someClass a; someOtherClass b; public: myClass() : a(DEFAULT_A) {} //implied is b() myClass(const myClass& mc) : a(mc.a) {} //implied is b(mc.b)??? or is it b()? } 回答1: Explicitly-defined copy constructors do

Why would a copy constructor have more than one parameter?

非 Y 不嫁゛ 提交于 2019-12-03 02:19:09
$12.8/2 - 'A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments (8.3.6).106)' So far, I have not come across any example of a situation where there is a need to declare a copy constructor with additional default parameters. Would like to know any real time use of such a copy constructor which take more than one parameter. The old std::basic_string does have one too: basic_string(const basic_string& s, size_type

Implementing the copy constructor in terms of operator=

ぃ、小莉子 提交于 2019-12-02 22:28:53
If the operator= is properly defined, is it OK to use the following as copy constructor? MyClass::MyClass(MyClass const &_copy) { *this = _copy; } If all members of MyClass have a default constructor, yes. Note that usually it is the other way around: class MyClass { public: MyClass(MyClass const&); // Implemented void swap(MyClass&) throw(); // Implemented MyClass& operator=(MyClass rhs) { rhs.swap(*this); return *this; } }; We pass by value in operator= so that the copy constructor gets called. Note that everything is exception safe, since swap is guaranteed not to throw (you have to ensure

What is the distinction between implicitly-declared and implicitly-defined copy constructors?

最后都变了- 提交于 2019-12-02 22:03:19
I am reviewing the cppreference page on copy constructors here: http://en.cppreference.com/w/cpp/language/copy_constructor I've read the 2 sections regarding implicitly-declared copy constructors and implicitly-defined copy constructors quite a few times but I still don't understand the distinction. Wouldn't an implicitly declared but NOT defined constructor result in linker problems? The rules are very complex. I don't remember there being a distinction in C++03: Either you had a compiler generated copy constructor or you didn't. Can someone explain (in simpler words) what the distinction

Create a deep copy of an array C++

 ̄綄美尐妖づ 提交于 2019-12-02 20:10:12
问题 I am try to solve some problems in my program and it would appear that there is either a problem with my copy constructor or with my destructor. I am getting a memory exception. any help would me appreciated Thanks ArrayStorage::ArrayStorage(const ArrayStorage &a):readArray(a.readArray),arraysize(a.arraysize) { readArray = new string[arraysize]; //create the array memcpy (readArray,a.readArray,sizeof(string)*arraysize);//Copy the values of bytes from the location pointed at by the souce and