copy-constructor

Questions about postblit and move semantics

♀尐吖头ヾ 提交于 2019-11-29 11:59:13
问题 I have already asked a similar question a while ago, but I'm still unclear on some details. Under what circumstances is the postblit constructor called? What are the semantics of moving an object? Will it be postblitted and/or destructed? What happens if I return a local variable by value? Will it implicitly be moved? How do I cast an expression to an rvalue? For example, how would a generic swap look like? 回答1: A postblit constructor is called whenever the struct is copied - e.g. when

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

别等时光非礼了梦想. 提交于 2019-11-29 11:29:20
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. SpaceCowboy proposes the idiomatic clone method, but overlooked 3 crucial details: class Super { public: virtual Super* clone() const { return new Super(*this); } }; class Child: public Super { public: virtual Child* clone()

Copy constructor curly braces initialization

若如初见. 提交于 2019-11-29 10:34:13
"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: struct Data { int mMember1; float mMember2; char mMember3; }; int main() { Data aData_1{1,0.3,33}; Data

How to declare copy constructor in derived class, without default construcor in base?

房东的猫 提交于 2019-11-29 08:25:21
问题 Please take a look on the following example: class Base { protected: int m_nValue; public: Base(int nValue) : m_nValue(nValue) { } const char* GetName() { return "Base"; } int GetValue() { return m_nValue; } }; class Derived: public Base { public: Derived(int nValue) : Base(nValue) { } Derived( const Base &d ){ std::cout << "copy constructor\n"; } const char* GetName() { return "Derived"; } int GetValueDoubled() { return m_nValue * 2; } }; This code keeps throwing me an error that there are

Templated copy-constructor fails with specific templated type

强颜欢笑 提交于 2019-11-29 06:46:55
As some of my code required implicit conversion between matrices of different types (e.g. Matrix<int> to Matrix<double> ), I defined a templated copy constructor Matrix<T>::Matrix(Matrix<U> const&) instead of the standard Matrix<T>::Matrix(Matrix<T> const&) : template <typename T> class Matrix { public: // ... template <typename U> Matrix(Matrix<U> const&); // ... private unsigned int m_rows, m_cols; T *m_data; // ... }; With an appropriate typecast added to the copy-constructor, this method flawlessly converted between matrices of different types. Surprisingly, it fails with a malloc error in

Why is the argument of the copy constructor a reference rather than a pointer?

本秂侑毒 提交于 2019-11-29 06:28:16
Why is the argument of the copy constructor a reference rather than a pointer? Why can't we use the pointer instead? Nicol Bolas There are many reasons: References cannot be NULL. OK, it's possible to create a NULL reference, but it's also possible to cast a std::vector<int>* into a std::vector<SomeType>* . That doesn't mean such a cast has defined behavior. And neither does creating a NULL reference. Pointers have defined behavior when set to NULL; references do not. References are therefore always expected to refer to actual objects. Variables and temporaries cannot be implicitly converted

compiler generated constructors [duplicate]

*爱你&永不变心* 提交于 2019-11-29 06:16:14
This question already has an answer here: Conditions for automatic generation of default/copy/move ctor and copy/move assignment operator? 3 answers This is just a quick question to understand correctly what happens when you create a class with a constructor like this: class A { public: A() {} }; I know that no default constructor is generated since it is already defined but are copy and assignment constructors generated by the compiler or in other words do i need to declare a private copy constructor and a private assignment operator in order to prevent this from happening? class A { private:

C++ Calling a copy constructor on an unknown derived class through an abstract base class

感情迁移 提交于 2019-11-29 06:12:50
I'm making a tree that has several different node types: a binary node, a unary node, and a terminal node. I've got an ABC that all the nodes inherit from. I'm trying to write a recursive copy constructor for the tree like so: class gpnode { public: gpnode() {}; virtual ~gpnode() {}; gpnode(const gpnode& src) {}; gpnode* parent; } class bnode:gpnode { public: bnode() {//stuff}; ~bnode() {//recursive delete}; bnode(const bnode& src) { lnode = gpnode(src.lnode); rnode = gpnode(src.rnode); lnode->parent = this; rnode->parent = this; } gpnode* lnode; gpnode* rnode; } class unode:gpnode { public:

Is is possible to use std::map in C++ with a class without any copy operator?

自闭症网瘾萝莉.ら 提交于 2019-11-29 06:02:44
I'm using a Class (Object) that doesn't have any copy operator : it basically cannot be copied right now. I have a std::map<int,Object> objects variable that lists objects with an int identifier. How could I add an Object to this map without having to use copy operators? I tried objects.insert(std::pair<0,Object()>); but that won't compile. I would just like to create my object initially inside the map using the default constructor, but writing objects[0]; fails... Thanks :) In C++03, objects that are stored in STL containers must be copyable. This is because a STL container's std::allocator

Why Copy Constructor is called here instead of normal Constructor and overloaded assignment operator? [duplicate]

折月煮酒 提交于 2019-11-29 02:37:06
Possible Duplicate: Is there a difference in C++ between copy initialization and direct initialization? Copy constructors and Assignment Operators I have a class C in which I have overloaded Normal, copy constructor and assignment operator to print a trace of what is being called.. I wrote following pieces of code to test what is being called when? C c1; --> Normal Constuctor .. // understood Fine C c2; c2 = c1; --> Normal constructor + assignment operator .. //understood Fine C * c3 = new C(C1) --> Copy constructor // Understood Fine C c4 = c1 --> copy constructor // Not Able to understand