Questions tagged [copy-assignment]

146 questions
2
votes
3 answers

dynamic memory allocation,pointer members and destructors

I wrote the following dummy class to understand how the copy constructor,the copy assignment operator and the destructor works: #include #include class Box { public: // default constructor Box(int i=10,const…
Luca
  • 1,658
  • 4
  • 20
  • 41
2
votes
1 answer

Generated copy and move operators?

Currently I read the book Effective Modern C++ from Scott Meyers, and now I'm at: Item 17: Understand special member function generation. My misunderstanding comes from the following part (rationale): The two copy operations are independent:…
p.i.g.
  • 2,815
  • 2
  • 24
  • 41
2
votes
2 answers

thread-safe copy assignment operator for smart pointer class

I'm implementing a smart pointer class, and having a couple of confusions. Would really appreciate if people could help me clarify. 1: I thought smart pointer class should have "new" in constructor, "delete" in desctructor. But I can't seem to find…
Sarah
  • 453
  • 7
  • 16
2
votes
1 answer

Operator overloading "+" & "=" issue -- school asst

first time poster here but lurker for a couple of months now. Currently indulging in C++ with a small amount of Java knowledge as well from prior Comp Sci courses at school. I apologize if some of you look at this and are disappointed since there…
1
vote
1 answer

Is there a Declarative approach to converting each member of a TypeScipt array to a full instance of an Object?

I have a class I can't change, simplified version shown. I got an array of variations on the class. I need to add the rest of the class to each member of the array before I can use them. class ValueType { name: string; size: number = 5; …
1
vote
1 answer

C++ implicitly declared move assignment operator calling implicitly declared copy assignment operator of base class

A simple question but I can not find the set of rules that proves that the behavior of the following code example is correct. It seems here that only strDerived is moved from b, but strBase is copied? Will the implicitly declared move assignment…
1
vote
1 answer

assigning a class variable outsite constructor body

I am very new to C++, and am following along with this tutorial and have come across this class constructor: class Player: public Entity { private: std::string m_Name; public: Player(const std::string& name) : m_Name(name) {} …
1
vote
3 answers

make template class with std::vector non-copyable when T is non-copyable

#include #include template class V { public: template, int> = 0> auto operator = (const V &rhs) -> V & { v = rhs.v; return *this; } private: …
slyx
  • 2,063
  • 1
  • 19
  • 28
1
vote
1 answer

Explicitly defaulted copy/move assignment operators implicitly deleted because field has no copy/move operators. C++

I'm new to C++ and dont know why this is happening and how to fix it. Here's some snippets of the code: header file: class Dictionary{ private: string filename; const string theSeparators; public: …
1
vote
1 answer

Problem with my Copy and Move constructor and assignment operators

I want to build my own full Vector class in C++. I started like this: #include #include #define Print(x)(std::cout<< x << std::endl) // Vector Class // template class Vector { // Attributes int…
1
vote
2 answers

How does the compiler copy member data that are arrays?

AFAIK, an array cannot be copied nor assigned so: int a[5] = {1, 2};// 1 2 0 0 0 int b = a;// error b = a; // error But how does the compiler copies arrays that are member data of a class/struct type through the trivial copy-ctor and…
Itachi Uchiwa
  • 3,044
  • 12
  • 26
1
vote
1 answer

C++ destructor called after calling constructor

By looking at the example: #include int wow=0; class Foo{ int cow = 0; public: Foo(){ std::cout << "Foo +\n"; cow = 0; ++wow; } Foo(int n){ std::cout << "Foo has " << n << "\n"; cow = n; ++wow; } …
itchyfeet
  • 43
  • 7
1
vote
2 answers

Use copy constructor while user-declared move constructor is present

I'm having some confusion towards using the copy constructor and copy assignment operator while dealing with unique_ptr. I would really appreciate it if you could share some insights! Now we have class B. Struct C is a member of class B. Struct C…
Skylar
  • 127
  • 8
1
vote
1 answer

Question about copy assignment and delete operator

I came across this link and I saw this program: #include template class Auto_ptr4 { T* m_ptr; public: Auto_ptr4(T* ptr = nullptr) :m_ptr(ptr) { } ~Auto_ptr4() { delete m_ptr; } …
cuong.pq
  • 137
  • 1
  • 1
  • 4
1
vote
1 answer

overloaded assignment operator not working

I have a class Employee. (Some of my comments are not updated from when I added members tasks and taskList; I apologize for that.) Employee.h #include #include using namespace std; class Employee { private: string…