If among the elements of my class I have also a const data member, how do copy constructor and assignment operator behave? I think, but I am not sure, that copy constructor is provided (as most cases) while assignment operator is not provided (differently from what happens normally) and so if I want to use it, I must implement it (of course not assigning the const data member)
CodePudding user response:
struct foo {
int const x;
};
foo f0{3}; // legal
foo f1 = f0; // legal, copy-construction
foo make_foo(int y) { return {y}; } // legal, direct-initialization
foo f2 = make_foo(3); // legal, elision and/or move-construction
f2 = f1; // illegal, copy-assignment
f2 = make_foo(3); // illegal, move-assignment
Construction and assignment are different operations. =
does not mean assignment always.
You can construct const
subobjects; you cannot assign to them. This property then applies to the object itself in its automatically written special member functions.