copy-constructor

c++: cast operator vs. assign operator vs. conversion constructor priority

北城余情 提交于 2019-12-05 03:31:15
Let's have this code: Test1 t1; Test2 t2; t1 = t2; I believe there are three (or more?) ways how to implement t1 = t2 to overload assign operator in Test1 to overload type cast operator in Test2 to create Test1(const Test2&) conversion constructor According to my GCC testing, this is the priority of what is used: assign operator conversion constructor and type cast operator (ambiguous) const conversion constructor and const type cast operator (ambiguous) Please help me understand why this priority. I use this code for testing (uncomment some lines to try out) struct Test2; struct Test1 { Test1

Call default copy constructor from within overloaded copy constructor

时光总嘲笑我的痴心妄想 提交于 2019-12-05 01:10:20
I need to write a copy constructor that deep copies the contents of a std::shared_ptr . However, there are a bunch of variable int a, b, c, d, e; also defined in the class. Is there a way to generate the default copy constructor code (or call the default copy constructor) inside my new overloaded one. Here is a code snippet with a comment that hopefully clarifies the issue. class Foo { public: Foo() {} Foo(Foo const & other); ... private: int a, b, c, d, e; std::shared_ptr<Bla> p; }; Foo::Foo(Foo const & other) { p.reset(new Bla(*other.p)); // Can I avoid having to write the default copy

SFINAE away a copy constructor

旧街凉风 提交于 2019-12-05 01:03:26
Under certain conditions, I'd like to SFINAE away the copy constructor and copy assignment operator of a class template. But if I do so, a default copy constructor and a default assignment operator are generated. The SFINAE is done based on tags I pass as class template parameters. The problem is, that SFINAE only works on templates and a copy constructor/assignment operator can't be a template. Does there exist a workaround? stefan This solution uses a base class that is conditionally not copyable (by explicitely marking the copy constructor and copy assignment operator as deleted). template

Constructor or Assignment Operator

限于喜欢 提交于 2019-12-05 00:17:42
问题 Can you help me is there definition in C++ standard that describes which one will be called constructor or assignment operator in this case: #include <iostream> using namespace std; class CTest { public: CTest() : m_nTest(0) { cout << "Default constructor" << endl; } CTest(int a) : m_nTest(a) { cout << "Int constructor" << endl; } CTest(const CTest& obj) { m_nTest = obj.m_nTest; cout << "Copy constructor" << endl; } CTest& operator=(int rhs) { m_nTest = rhs; cout << "Assignment" << endl;

Are there any use cases for a class which is copyable but not movable?

大兔子大兔子 提交于 2019-12-04 23:41:15
After reading this recent question by @Mehrdad on which classes should be made non-movable and therefore non-copyable , I starting wondering if there are use cases for a class which can be copied but not moved . Technically, this is possible: struct S { S() { } S(S const& s) { } S(S&&) = delete; }; S foo() { S s1; S s2(s1); // OK (copyable) return s1; // ERROR! (non-movable) } Although S has a copy constructor, it obviously does not model the CopyConstructible concept, because that is in turn a refinement of the MoveConstructible concept, which requires the presence of a (non-deleted) move

Derived and base class, can I set the base explicitly?

流过昼夜 提交于 2019-12-04 22:53:26
public class SuperCar: Car { public bool SuperWheels { get {return true; } } } public class Car { public bool HasSteeringWheel { get {return true;} } } How can I set the base class for the derived Supercar? For example, I want to simply set SuperCars base class like this: public void SetCar( Car car ) { SuperCar scar = new SuperCar(); car.Base = car; } Basically, if I have Car objects, I do not want to manually iterate through every property of the car in order to setup the SuperCar oject, which I think is the only way you can do it but if you can do it the other way it would be sooo much

Explicit copy constructor and uniform initialization

不羁岁月 提交于 2019-12-04 22:19:54
Explicit copy constructors disallow something like Foo foo = bar; , and enforce the copy usage as Foo foo(bar); . In addition, explicit copy constructors also disallow returning objects by value from a function. However, I tried replacing the copy initialization with braces, like so struct Foo { Foo() = default; explicit Foo(const Foo&) = default; }; int main() { Foo bar; Foo foo{bar}; // error here } and I am getting the error (g++5.2) error: no matching function for call to 'Foo::Foo(Foo&)' or (clang++) error: excess elements in struct initializer Removing the explicit makes the code

“Almost default” copy constructor (& assignment operator) in C++

孤人 提交于 2019-12-04 18:36:48
问题 A common thing I find myself doing is making "almost default" copy constructors and assignment operators. That is, I find myself in situations where the compiler supplied copy and assignment operators would work for most of the data members, but there's a particular data member which needs to be handled differently. This means that I have to explicitly create a copy constructor/assignment operator, including explicitly listing all the data members which have simple copy semantics. This can

Copy constructor is called many times when constructing a thread by function object

旧街凉风 提交于 2019-12-04 13:38:19
问题 I try to pass a function object to a thread. I am confused when I found the copy constructor is called two times in the 'main' thread. Why not simply copy once instead of twice? The second copy is useless. Code Snippet: #include <iostream> #include <thread> using namespace std; struct A { A() { cout << "constructor this=" << this << " thread_id=" << this_thread::get_id() << endl; } A(const A &other) { cout << "Copy constructor this=" << this << " thread_id=" << this_thread::get_id() << endl;

Error when have private copy ctor with public assignment operator

北城余情 提交于 2019-12-04 12:10:51
问题 Can one of you explain why the following piece of code does not compile? #include <iostream> using namespace std; class Foo { public: Foo() { cout << "Foo::Foo()" << endl << endl; } Foo& operator=(const Foo&) { cout << "Foo::operator=(const Foo&)" << endl << endl; } private: Foo(const Foo& b) { *this = b; cout << "Foo::Foo(const Foo&)" << endl << endl; } }; int main() { Foo foo; foo = Foo(); } The error I receive: $ g++ -o copy_ctor_assign copy_ctor_assign.cc && ./copy_ctor_assign copy_ctor