copy-constructor

Copy Constructor for pointers to objects

元气小坏坏 提交于 2019-12-12 05:52:42
问题 I am having problem in writing copy constructor for pointers to objects. This is my exact problem I have a class G1 that has an object s1 as its private data member. This is an object of a struct. The struct is composed of a vector<pair<int,pointer to another object of a different class>>. Now when I create a pointer for G1 everything is fine. When I try to copy this pointer to another new pointer of the same class it is making a shallow copy. So when I try to delete the first pointer the

Explicit and non-explicit constructors

懵懂的女人 提交于 2019-12-12 04:43:55
问题 class Test { public: Test(int i) { cout<<"constructor called\n";} Test(const Test& t) { cout<<" copy constructor called\n";} }; class Test1 { public: Test1(int i) { cout<<"constructor called\n";} explicit Test1(const Test1& t) { cout<<" copy constructor called\n";} }; int main() { Test t(0); Test u = 0; //Test1 t1(0); Line 1 //Test1 u1 = 0; Line 2 } I observe different outputs. Case 1: When Line 1 and Line 2 are commented the o/p is : constructor called constructor called Case 2: When Line 1

Deleted vs empty copy constructor

若如初见. 提交于 2019-12-12 03:17:47
问题 Examples of empty and deleted copy constructors: class A { public: // empty copy constructor A(const A &) {} } class B { public: // deleted copy constructor A(const A&) = delete; } Are they doing the same in practice (disables copying for object)? Why delete is better than {} ? 回答1: Are they doing the same in practice (disables copying for object)? No. Attempting to call a deleted function results in a compile-time error. An empty copy constructor can be called just fine, it just default

Conversion constructor with responsibility transfer

痴心易碎 提交于 2019-12-12 02:39:21
问题 This is a follow up of this question. I need to implement a conversion operation to pass responsibility for a member object from one object to another. The conversion operation takes place in a class hierarchy. There is a Base class and two deriving classes, say Child1 and Child2 . In the Base class, there is a dynamically created object I need to pass from Child1 to Child2 (along with responsibility) while the conversion happens and not let Child1 's destructor destroy it. I've written a

C++ Object Composition, Depdendency Injection and Copy Constructors

被刻印的时光 ゝ 提交于 2019-12-11 19:43:24
问题 I want to design a class in C++ called Entity. That entity has a pointer to a member of Vector3D which is the position of the entity in 3D space. The constructor allows a pointer of type Vector3D to be passed to the constructor, such that the Vector3D instance is instantiated outside of the class. Because there is a pointer to a dynamically allocated object, the copy constructor and assignment operator must be overloaded to deep copy the vector. However, because the vector can be passed in

weird C++ constructor/copy constructor issues in g++

妖精的绣舞 提交于 2019-12-11 10:15:51
问题 #include <iostream> using namespace std; class X { public: X() { cout<<"Cons"<<endl; } X(const X& x){ cout<<"Copy"<<endl; } void operator=(const X& x){ cout<<"Assignment called"; } }; X& fun() { X s; return s; } int main(){ X s = fun(); return 0; } This code calls the copy constructor also. Why does this work? I recall that the first time I ran this program, it seg faulted. But after a while, it started calling this copy cons. and now works!! Wierd. But if I replace, fun() as follows: X fun()

implementing stack with a linked list in C++ , copy constructor

心不动则不痛 提交于 2019-12-11 07:12:57
问题 I am trying to implement a stack using a linked list in C++, and I don't know how to write correctly the copy constructor of the stack class. I am using the following classes: class Node{ int m_num; Node* m_pNext; public: Node(); ~Node(); //and the standard get&set functions.. } class LinkedList{ int m_size; Node* m_pHead; public: LinkedList(); LinkedList(const LinkedList& obj); ~LinkedList(); //and the standard get&set functions.. } class Stack{ LinkedList m_stack; public: Stack(); Stack

Difference between two ways of declaring an object on the stack

家住魔仙堡 提交于 2019-12-11 02:37:55
问题 What's the difference between the following two declarations, assuming I have not specified a copy constructor and operator= in class Beatle ? Beatle john(paul); and Beatle john = paul; Edit: In objects assignment, the operator = implicitly calls the copy constructor unless told otherwise? 回答1: They're different grammatical constructions. The first one is direct initialization , the second is copy initialization . They behave virtually identically, only that the second requires a non-

What is the effect of 'explicit' keyword on the Return Value Optimization (RVO)?

Deadly 提交于 2019-12-11 02:07:25
问题 Following code works perfectly fine (showing RVO): struct A { A (int) { cout << "A::A()\n"; } // constructor A (const A&) { cout << "A::A(const A&)\n"; } // copy constructor }; A foo () { return A(0); } int main () { A a = foo(); } Output: A::A() // --> which means copy constructor is not called If I mark the copy constructor as explicit : explicit A (const A&) { ... } Then the compiler errors out: explicit.cpp: In function ‘A foo()’: explicit.cpp:10:22: error: no matching function for call

C++ maintaining a mixed collection of subclass objects

时光总嘲笑我的痴心妄想 提交于 2019-12-11 01:49:08
问题 Apologies if I'm missing a fairly fundamental concept here, but I'm trying to work out how to maintain a collection of multiple class types (all derived from the same parent) and still have access to their subclass-specific methods when retrieving them from the collection. As context, I have one base class ("BaseClass") and a number of classes (say "SubClassA" through "SubClassZ") that all derive from this base class. I also need to maintain a central indexed collection of all SubClass