copy-constructor

Why does an in-place member initialization use a copy constructor in C++11?

前提是你 提交于 2019-12-18 11:47:27
问题 I'm a little bit confused about the following code: struct A { std::atomic<int> a = 0; }; Which gives an error: copying member subobject of type 'std::atomic' invokes deleted constructor But almost the same code does work: struct A { std::atomic<int> a = {0}; }; Okey, if the first variant requires the copy constructor, then it have to use operator=() . But wait! This operator perfectly works without the copy constructor: A a; a.a = 1; Can anyone explain how both of the in-place

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

[亡魂溺海] 提交于 2019-12-18 04:42:30
问题 Why is the argument of the copy constructor a reference rather than a pointer? Why can't we use the pointer instead? 回答1: 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

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

梦想的初衷 提交于 2019-12-18 04:41:46
问题 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 :) 回答1: In C++03,

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

ⅰ亾dé卋堺 提交于 2019-12-18 04:41:23
问题 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 :) 回答1: In C++03,

Why does the implicit copy constructor calls the base class copy constructor and the defined copy constructor doesn't?

穿精又带淫゛_ 提交于 2019-12-17 23:19:45
问题 Consider a class hierarchy where A is the base class and B derives from A . If the copy constructor is not defined in B , the compiler will synthesize one. When invoked, this copy constructor will call the base class copy constructor (even the synthesized one, if none has been provided by the user). #include <iostream> class A { int a; public: A() { std::cout << "A::Default constructor" << std::endl; } A(const A& rhs) { std::cout << "A::Copy constructor" << std::endl; } }; class B : public A

Copy constructors and Assignment Operators

坚强是说给别人听的谎言 提交于 2019-12-17 16:40:41
问题 I wrote the following program to test when the copy constructor is called and when the assignment operator is called: #include class Test { public: Test() : iItem (0) { std::cout << "This is the default ctor" << std::endl; } Test (const Test& t) : iItem (t.iItem) { std::cout << "This is the copy ctor" << std::endl; } ~Test() { std::cout << "This is the dtor" << std::endl; } const Test& operator=(const Test& t) { iItem = t.iItem; std::cout << "This is the assignment operator" << std::endl;

Why C++ copy constructor must use const object?

让人想犯罪 __ 提交于 2019-12-17 15:35:13
问题 I understand that when we define a class copy constructor of the class is necessary as Rule of three states. I also notice that the argument of the copy constructor is usually const as the following codes illustrate: class ABC { public: int a; int b; ABC(const ABC &other) { a = other.a; b = other.b; } } My question is what would happen if the argument of the copy constructor is not const: class ABC { public: int a; int b; ABC(ABC &other) { a = other.a; b = other.b; } } I understand that in

C++ copy-construct construct-and-assign question

跟風遠走 提交于 2019-12-17 14:01:44
问题 Here is an extract from item 56 of the book "C++ Gotchas": It's not uncommon to see a simple initialization of a Y object written any of three different ways, as if they were equivalent. Y a( 1066 ); Y b = Y(1066); Y c = 1066; In point of fact, all three of these initializations will probably result in the same object code being generated, but they're not equivalent. The initialization of a is known as a direct initialization, and it does precisely what one might expect. The initialization is

Explicit copy constructor

一个人想着一个人 提交于 2019-12-17 07:39:46
问题 I have extended std::string to fulfil my needs of having to write custom function build into string class called CustomString I have defined constructors: class CustomString : public std::string { public: explicit CustomString(void); explicit CustomString(const std::string& str); explicit CustomString(const CustomString& customString); //assignment operator CustomString& operator=(const CustomString& customString); ... }; In the third constructor (copy constructor) and assignment operator,

How do I make this C++ object non-copyable?

对着背影说爱祢 提交于 2019-12-17 07:09:40
问题 See title. I have: class Foo { private: Foo(); public: static Foo* create(); } What need I do from here to make Foo un-copyable? Thanks! 回答1: class Foo { private: Foo(); Foo( const Foo& ); // non construction-copyable Foo& operator=( const Foo& ); // non copyable public: static Foo* create(); } If you're using boost, you can also inherit from noncopyable : http://www.boost.org/doc/libs/1_41_0/boost/noncopyable.hpp EDIT: C++11 version if you have a compiler supporting this feature: class Foo {