Questions tagged [copy-assignment]
146 questions
2
votes
1 answer
C++: What is the output, if no optimizations are applied to these different ways to create/initialize, copy, assign?
I found a bit of confusion in the ways a variable is constructed, copied, assigned because in the compilers I tried they usually apply some kind of optimization (remove temporary etc.).
I'm listing the different ways I tried and the output of my…

pasha
- 2,035
- 20
- 34
2
votes
1 answer
placement new to circumvent assignment constructor
Is it a viable solution to use placement new to circumvent copy assignment?
I have a member object that contains const members.
The object itself is meant to be created at runtime, but it's members are meant to be const.
I was wondering whether I…

Raildex
- 3,406
- 1
- 18
- 42
2
votes
1 answer
Why delete of templete copy constructor cause assignment operator disfunctional?
I have code like below which looks a little bit Confusing. I define a template class. It has a user-defined constructor. When I declare two objects of this template class by "operator =", its user-defined contructor is called to my surprise.…

Caiyi Zhou
- 143
- 1
- 9
2
votes
1 answer
Copy assignment operator= is not called
Why overloaded operator (operator=, specifically) is not called in this case?
#include
using namespace std;
class mc{
public:
mc()=default;
mc(mc& that){cout<<1;} //simplified copy constructor
mc& operator=(mc&…

o_yeah
- 688
- 7
- 17
2
votes
4 answers
Copy struct to array with const values
I'm trying to replace a struct value that has a const value in a non const array but it fails to compile with:
Object of type 'Foo' cannot be assigned because its copy assignment operator is implicitly deleted
Here is an example:
struct Foo {
…

utahwithak
- 6,235
- 2
- 40
- 62
2
votes
2 answers
C4512 assignment operator could not be generated
I'm working on updating an old C++ application, it is being compiled with MSVC 2013, not the latest I know.
I'm getting a warning:
1>c:\fraenkelsoftware-millikan\shared\debug\debughelper.h(84): warning C4512: 'HowLong' : assignment operator could…

SPlatten
- 5,334
- 11
- 57
- 128
2
votes
0 answers
Why & qualifier is used with assignment operator?
My question is in reference to the following links:
Why declare a deleted assignment operators with the ref-qualifier &
Should I use lvalue reference qualifiers for assignment operators?
I could not follow the posts. Could somebody explain:
What…

SNR_BT
- 133
- 5
2
votes
2 answers
Does object have to be destroyed when a new object is assigned to its address?
Consider following code.
#include
using namespace std;
constexpr size_t TOTAL = 2;
class thing
{
private:
inline static size_t NEXT_ID = 0;
size_t id;
public:
thing() : id(NEXT_ID++)
{
printf("Thing %zd…

sanitizedUser
- 1,723
- 3
- 18
- 33
2
votes
4 answers
Using copy assignment in derived class
The cppreference says:
Because the copy assignment operator is always declared for any class, the base class assignment operator is always hidden. If a using-declaration is used to bring in the assignment operator from the base class, and its…

Heyi Sun
- 23
- 3
2
votes
1 answer
Pass by value vs. pass by reference in copy assignment operator
First and foremost, there's a similar popular post What is the copy-and-swap idiom?.
The accepted answer has a link to https://web.archive.org/web/20140113221447/http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/.
Both the accepted and…

24n8
- 1,898
- 1
- 12
- 25
2
votes
3 answers
copy assignment operator clarification
Im writing a copy assignment operator for a class I've created and I'm using a previous post as a guide:
What is The Rule of Three?
Im a bit confused on one aspect of this person's explanation.
Here is their class:
class person
{
char* name;
…

MagicGAT
- 135
- 7
2
votes
1 answer
Should I use placement-new in a copy assignment operator
I'm creating a discriminated union-like class. I am using c++ 17, so I could technically use a std::variant, but because of the specific use-case I want the meaning of each variant to be more explicit (especially because two of the cases hold no…

Anonymous
- 491
- 2
- 12
2
votes
1 answer
CRTP & copy/move assignment/constructor inheritance
I am trying to implement a move/copy assignment operators and constructors in a base class for the derived classes using CRTP.
template
class base {
public:
Derived& operator= (const Derived& other) {
// Copy the base…

egst
- 1,605
- 3
- 18
- 25
2
votes
1 answer
How to swap two objects without copy assignment operator?
I have a class A where, the copy assignment operator is deleted. How should I swap two instances of A ?
I tried using std::swap but that did not work.
class A {
private:
int a;
public:
A& operator=(const A& other) = delete;
A(int _a =…

rranjik
- 690
- 9
- 21
2
votes
6 answers
Implementation supplied copy constructor and assignment operator
I have a small confusion regarding the situations where the implementation (compiler) will not supply the copy constructor and the copy assignment operator.
When we declare the copy ctor and/or copy assignment operator in our class.
Some says when…

riderchap
- 667
- 2
- 11
- 19