copy-constructor

Why does the map.insert() method invoke the copy constructor twice?

匆匆过客 提交于 2021-02-19 02:39:08
问题 I'm creating the custom class Node in order to implement a binary tree using a map<int,Node> container: the int key of the map is the identifier of a Node object. In the class Node I had to implement a copy constructor. When inserting a Node object on the map, I noticed that the copy constructor of the Node is invoked twice. Why? cout << "node2" << endl; Node node2; node2.set_depth(2); node2.make_it_branch(3,4); cout << "map" << endl; map<int,Node> mapping; cout << "toInsert" << endl; pair

Who manages the exception thrown by a copy constructor in parameters? [duplicate]

狂风中的少年 提交于 2021-02-19 01:35:07
问题 This question already has an answer here : Constructor with by-value parameter & noexcept (1 answer) Closed 1 year ago . Assume I have this function void foo() noexcept { // Safely noexcept code. } And then this class: class Bar { Bar(const Bar&) { ... } // Is not noexcept, so might throw // Non movable: Bar(Bar&&) = delete; }; Now, I need to modify foo() to receive a Bar by value: void foo(Bar bar) // noexcept? { // Safely noexcept code } I assume the copy of Bar is done before the call to

Why doesn't the standard consider a template constructor as a copy constructor?

▼魔方 西西 提交于 2021-02-17 18:39:47
问题 Here's the definition of copy constructor, [class.copy.ctor/1]: 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 ([dcl.fct.default]). Why does the standard exclude templates as copy constructors? In this simple example, both constructors are copy constructors: struct Foo { Foo(const Foo &); // copy

Why does the initialisation of an object invoke the copy constructor?

梦想与她 提交于 2021-02-16 16:10:40
问题 Consider the following minimal working example: #include <atomic> int main() { ::std::atomic<bool> a = false; } Copy ctor and copy assignment of atomic are both explicitly deleted. However, this should invoke the ctor taking exactly a bool. Both g++ and clang++ complain that this line is attempting to invoke the copy ctor of atomic : $ g++ -std=c++1z a.cpp a.cpp: In function ‘int main()’: a.cpp:4:27: error: use of deleted function ‘std::atomic<bool>::atomic(const std::atomic<bool>&)’ ::std:

How to use both default and custom copy constructor in C++?

送分小仙女□ 提交于 2021-02-16 15:56:18
问题 I have a long class with a lot of data members. I want to write a copy constructor for it. But, if I write my own copy constructor, I lose access to the default copy constructor. I just want to repair a few pointers in my own copy constructor. So I want to have a shallow copy of the object which can be done by the default copy constructor. Is there a possibility to access the default copy constructor when I have my own copy constructor? 回答1: Wrap the things you don't want to change in a

Call to implicitly deleted copy constructor

感情迁移 提交于 2021-02-11 12:13:04
问题 I have the following setup: class MyClass { public: static MyClass Clone(const MyClass& other) { return MyClass(other, 10); } static MyClass CreateNormal(int x, int y) { return MyClass(int x, int y); } // There are a few more factory functions. private: // Constructor 1 MyClass(int x, int y) : b_(x, y) {} // Constructor 2 MyClass(const MyClass& other, int c) : b_(other.b_, c) {} // And a lot more constructors. // NotMyClass has its copy constructor deleted. NotMyClass b_; } int main() {

Explicit Copy constructor call syntax

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-11 06:28:17
问题 When I declare my copy constructor as explicit, calling it using = instead of () doesn't compile. Here's my code: class Base { public: explicit Base(){cout<<__PRETTY_FUNCTION__<<endl;} explicit Base(Base& b){cout <<__PRETTY_FUNCTION__<<endl;} }; int main() { Base a; Base b=a; } The compiler says: error: no matching function for call to ‘Base::Base(Base&)’ If I change it to Base b(a); It compiles fine. I thought C++ considers these two styles of instantiations the same. If I remove the

Explicit Copy constructor call syntax

冷暖自知 提交于 2021-02-11 06:27:25
问题 When I declare my copy constructor as explicit, calling it using = instead of () doesn't compile. Here's my code: class Base { public: explicit Base(){cout<<__PRETTY_FUNCTION__<<endl;} explicit Base(Base& b){cout <<__PRETTY_FUNCTION__<<endl;} }; int main() { Base a; Base b=a; } The compiler says: error: no matching function for call to ‘Base::Base(Base&)’ If I change it to Base b(a); It compiles fine. I thought C++ considers these two styles of instantiations the same. If I remove the

What happens when a new object is created using another (existing) object?

巧了我就是萌 提交于 2021-02-08 09:45:13
问题 I read in a book, it says: when we initialize a newly created object using another - uses copy constructor to create a temporary object and then uses assignment operator to copy values to the new object! And later in the book I read: When a new object is initialized using another object, compiler creates a temporary object which is copied to the new object using copy constructor. The temporary object is passed as an argument to the copy constructor. Really confused, what actually happens!!

What constructor or operator is used in a return (C++)

北城余情 提交于 2021-02-07 19:18:36
问题 I run this code for experimenting copy constructor and assignment operator class AClass { private: int a; public: AClass (int a_) : a(a_) { cout << " constructor AClass(int) " << a << endl; } AClass(const AClass & x) : a(x.a) { cout << " copy constructor AClass(const AClass &) " << a << endl; } AClass & operator=(const AClass & x) { a = x.a; cout << " AClass& operator=(const AClass &) " << a - endl; return *this; } }; AClass g () { AClass x(8); return x; } int main () { cout << " before