Questions tagged [copy-assignment]
146 questions
-1
votes
2 answers
Why Assignment Operator implicitly deleted for const-members in c++?
This is not a duplicate of the question attached to the close request.
I have a class with a const-qualified field. I'm trying to assign it to a data structure that already has a value in the place I'm assigning. But because my class has a…

JoeVictor
- 1,806
- 1
- 17
- 38
-1
votes
2 answers
C++ copy assignment operator behaviour
I used the code below to test the behaviour of copy assignment operator:
#include
using namespace std;
int group_number = 10; // Global
class Player {
public:
explicit Player(const int &g): group(g)
{
}
Player& operator=(const…

ZR_xdhp
- 361
- 2
- 13
-1
votes
1 answer
member vector with base class pointers
I have a class with a vector as below:
#include
class Base{};
class Derived: public Base{};
class Foo{
private:
std::vector vec;
public:
Foo() = default;
void addObject(const Base* b){
//…

Shwalala
- 25
- 5
-1
votes
1 answer
Could someone explain the meaning of vector()
I'm solving the Clone Graph in Leetcode, but I encountered a problem inside the following codes
class Node {
public:
int val;
vector neighbors;
Node() {
val = 0;
neighbors = vector();
}
Node(int…

o o
- 145
- 6
-1
votes
1 answer
Why does assignment to a reference use the copy assignment operator?
For the following example:
#include
using namespace std;
class Obj {
public:
Obj& operator=(const Obj& o) {
cout << "Copy assignment operator called" << endl;
return *this;
}
};
Obj o;
int…

johnnyodonnell
- 1,838
- 3
- 16
- 34
-1
votes
2 answers
c++ copy assignment and move assignment are not being called
I am trying to implement copy and move assignments, but I don't understand how should I use them. I have read the following topic
When did copy assignment operator called?
But it did not work for me.
Class:
class Directory{
string…

Alex Lavriv
- 321
- 1
- 10
-1
votes
1 answer
synthesized default constructor as deleted
I read this in the book:
The synthesized copy-assignment operator is defined as deleted if a
member has a deleted or inaccessible copy-assignment operator, or if
the class has a const or reference member.
And why we can not use reference type?

Zeukis
- 1,295
- 2
- 9
- 8
-1
votes
1 answer
Different ways to use default argument to a constructor in C++
What is the difference between the following three pieces of code with respect to MSVC?
Code 1:
Foo ctor defined as:
Foo::Foo(Bar &bar = Bar());
Foo ctor used as:
Foo foo = new Foo();
Code 2:
Foo ctor defined as:
Foo::Foo()
{
Bar bar = Bar();
…

Bonton255
- 2,231
- 3
- 28
- 44
-1
votes
2 answers
understanding copy constructor and copy assignment operator
According to me in case 1 copy assignment operator is used so output should be 0 68 but it is 0 87 and in case 2 it is 87 87 which is fine.
#include
using namespace std;
class numbered
{
static int un;
public:
int a;
numbered ():…

user3134167
- 375
- 1
- 2
- 10
-1
votes
1 answer
copy assignment operator of class
#include
using namespace std;
class ClassA
{
const int a;
int b, c;
public:
ClassA(int x, int y):a(10)
{
b = x;
c = y;
}
ClassA():a(10)
{
}
void print()
{
cout << a << endl;
…

nagaradderKantesh
- 1,672
- 4
- 18
- 30
-2
votes
1 answer
Copy assignment operator, vector
I have this vector that hold values (Not on the Heap!)
std::vector menu;
I want to implement copy assignment operator like this:
Restaurant &Restaurant::operator=(Restaurant &&other) {
if (this == &other)
return *this;
…

Sprint21
- 89
- 6