Questions tagged [copy-assignment]

146 questions
0
votes
1 answer

Why isn't the copy assingnment operator called when the copy construcotr is not available in C++?

Why when let's say I have an object declared like this: Obj o1; which is initialized by the default constructor (not very important here, how was o1 initialized, the point is it was initialized) and I create another object in this manner: Obj o2 =…
0
votes
0 answers

copy sematics for callable objects involving "this" pointer

I have a class member which is of type std::function that binds to this pointer by using std::bind. I implemented assignment operator which must also copy the callable object, however issue is in that, which this should be copied? this this or other…
metablaster
  • 1,958
  • 12
  • 26
0
votes
1 answer

Value assignment isn't working as expected

The assignment isn't working as I want. I'm trying to get the convergence of my calculations, so I'm iterating the function through the for loop, and I want break the loop when I get the tolerance. The function gives me an array D[nr, nz], I iterate…
0
votes
1 answer

constructing a std::function object from a type with a variadic template constructor

I have a templated class which instantiates an internal object of the template type and its constructor forwards the arguments to the internal object. template struct B { template B(Args&&... args) :…
0
votes
0 answers

Copy And Assignment: How to Deep Copy? C++

I have two classes that are essentially string classes that look like this. The first class holds a string as a member and the second class also holds a string plus an array of pointers for the 'MenuItem' class and also a tracker. const int…
0
votes
1 answer

The difference between initialize a object and assign to an object

I'm working on some basic object practice and I faced this problem. This class is named Matrix, and I tried to initialize a7 to a2.mutiply(a5). But the result is quite different when I tried to intialize a7 at first, and assign it to a2.mutiply(a5)…
0
votes
0 answers

Proper way to implement copy-assignment operator

I am trying to understand the correct way to implement the copy-assignment operator =. So I've implemented this example: class Window { public: Window(const std::string& = std::string()); Window(const Window&); Window& operator=(const…
Itachi Uchiwa
  • 3,044
  • 12
  • 26
0
votes
0 answers

Copy Assignment constructor in c++

I get this error while trying to create a copy assignment constructor: "overloaded operator must be a binary operator" this is my code: header: class User{ public: User(const std::string& name); const User& operator=(const User &other); …
Maya Saias
  • 37
  • 9
0
votes
0 answers

C++ assignment by value, what are the drawbacks?

Recently, I have stumbled over this pattern: class Foo { Foo() = default; Foo(const Foo &) = default; Foo(Foo &&) = default; Foo & operator=(Foo rhs) { swap(rhs); return *this; } }; A close look at this pattern revealed…
0
votes
2 answers

Assigment Operator for objects with user defined pointer members

I am trying to implement an operator= in C++ for an object which has as a member a pointer to a user defined type which also has dynamic memory allocated in it. So given the code below, how would on implement a correct operator= for B? What I am…
I Radu
  • 11
  • 2
0
votes
0 answers

Copy assignment not chosen

Given the following code snippet, why isn't the copy assignment operator chosen? It doesn't compile complaining about the copy constructor being deleted. I tried with a user declared assignment operator, but it still didn't work. So it isn't an…
scx
  • 3,221
  • 1
  • 19
  • 37
0
votes
2 answers

recursive call in copy constructor

I implemented a class following the rule of three, and I am getting a crash. Upon debugging I have came to the conclusion that the copy constructor is calling itself repeatedly instead of calling the equality operator. Why is this so? Shouldn't it…
0
votes
1 answer

Why Implementation for is_copy_assignable doesn't work?

This is my attempt at an implementation for is_copy_assignable: template struct IsCopyAssignable : std::false_type {}; template struct IsCopyAssignable::type =…
KeyC0de
  • 4,728
  • 8
  • 44
  • 68
0
votes
1 answer

Doesn't work Copy Assignment Operator

In my code, I am having trouble on my copy assignment operator. When I try to execute "=" operator in my main(), the content of my source array (numArr) is not copied to my destenation array (numArr2). Then, for my doubleCap() function, I am try to…
0
votes
3 answers

Is it safe to copy a class into uninitialized memory?

I have to use malloc to allocate memory. I have a custom class that needs a custom operator=. Let's say it is A: class A { public: int n; A(int n) : n(n) {} A& operator=(const A& other) { n = other.n; return *this; } }; I allocate…