copy-constructor

How to approach copying objects with smart pointers as class attributes?

99封情书 提交于 2019-11-30 14:27:24
From the boost library documentation I read this: Conceptually, smart pointers are seen as owning the object pointed to, and thus responsible for deletion of the object when it is no longer needed. I have a very simple problem: I want to use RAII for pointer attributes of a class that is Copyable and Assignable. The copy and assignment operations should be deep: every object should have its own copy of the actual data. Also, RTTI needs to be available for the attributes (their type may also be determined at runtime). Should I be searching for an implementation of a Copyable smart pointer (the

Non-const copy constructor and implicit conversions on return value

≡放荡痞女 提交于 2019-11-30 14:08:53
问题 Consider the following C++ code: struct B { }; struct A { A(int); A(A&); // missing const is intentional A(B); operator B(); }; A f() { // return A(1); // compiles fine return 1; // doesn't compile } This compiles fine on MSVC++ 2010 (in fact, on MSVC it even works if I remove B altogether). It doesn't on GCC 4.6.0: conv.cpp: In function ‘A f()’: conv.cpp:13:9: error: no matching function for call to ‘A::A(A)’ conv.cpp:13:9: note: candidates are: conv.cpp:6:2: note: A::A(B) conv.cpp:6:2: note

Strange behavior of copy-initialization, doesn't call the copy-constructor!

可紊 提交于 2019-11-30 13:44:56
I was reading the difference between direct-initialization and copy-initialization (§8.5/12): T x(a); //direct-initialization T y = a; //copy-initialization What I understand from reading about copy-initialization is that it needs accessible & non-explicit copy-constructor , or else the program wouldn't compile. I verified it by writing the following code: struct A { int i; A(int i) : i(i) { std::cout << " A(int i)" << std::endl; } private: A(const A &a) { std::cout << " A(const A &)" << std::endl; } }; int main() { A a = 10; //error - copy-ctor is private! } GCC gives an error ( ideone )

copy constructor with default arguments

天大地大妈咪最大 提交于 2019-11-30 11:32:49
As far as I know, the copy constructor must be of the form T(const T&) or T(T&) . What if I wanted to add default arguments to the signature? T(const T&, double f = 1.0); Would that be standards compliant? Yes. §[class.copy]/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 [ Example: X::X(const X&) and X::X(X&,int=1) are copy constructors. You can just create two different constructors: T(const T&) T

In which situations is the C++ copy constructor called?

大城市里の小女人 提交于 2019-11-30 10:23:01
问题 I know of the following situations in c++ where the copy constructor would be invoked: when an existing object is assigned an object of it own class MyClass A,B; A = new MyClass(); B=A; //copy constructor called if a functions receives as argument, passed by value, an object of a class void foo(MyClass a); foo(a); //copy constructor invoked when a function returns (by value) an object of the class MyClass foo () { MyClass temp; .... return temp; //copy constructor called } Please feel free to

Non-const copy constructor and implicit conversions on return value

别说谁变了你拦得住时间么 提交于 2019-11-30 09:35:43
Consider the following C++ code: struct B { }; struct A { A(int); A(A&); // missing const is intentional A(B); operator B(); }; A f() { // return A(1); // compiles fine return 1; // doesn't compile } This compiles fine on MSVC++ 2010 (in fact, on MSVC it even works if I remove B altogether). It doesn't on GCC 4.6.0: conv.cpp: In function ‘A f()’: conv.cpp:13:9: error: no matching function for call to ‘A::A(A)’ conv.cpp:13:9: note: candidates are: conv.cpp:6:2: note: A::A(B) conv.cpp:6:2: note: no known conversion for argument 1 from ‘A’ to ‘B’ conv.cpp:5:2: note: A::A(A&) conv.cpp:5:2: note:

Inheriting copy and move constructors of base class using “using” keyword

戏子无情 提交于 2019-11-30 09:33:55
问题 I want to inherit copy constructor of the base class using using keyword: #include <iostream> struct A { A() = default; A(const A &) { std::cerr << __PRETTY_FUNCTION__ << std::endl; } A( A &&) { std::cerr << __PRETTY_FUNCTION__ << std::endl; } A& operator=(const A &) { std::cerr << __PRETTY_FUNCTION__ << std::endl; return *this; } A& operator=( A &&) { std::cerr << __PRETTY_FUNCTION__ << std::endl; return *this; } }; struct B : A { using A::A; using A::operator=; B& operator=(const B &) { std

Templated copy-constructor fails with specific templated type

大憨熊 提交于 2019-11-30 08:36:14
问题 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

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

巧了我就是萌 提交于 2019-11-30 08:25:31
问题 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

compiler generated constructors [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-11-30 08:25:30
问题 This question already has answers here : Conditions for automatic generation of default/copy/move ctor and copy/move assignment operator? (3 answers) Closed 2 years ago . 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