Questions tagged [copy-assignment]

146 questions
1
vote
2 answers

C++ assign values individually or create a new object?

If I have a glm::vec3 for example and I want to assign a new value to it, which of the following methods is faster? vec = glm::vec3(1, 2, 3); or vec.x = 1; vec.y = 2; vec.z = 3; If I understand correctly, the first method does the…
Jojolatino
  • 696
  • 5
  • 11
1
vote
0 answers

copy assignment operator besides template

I have a class class A : public B { public: // 1st copy assignment with parameter pack template A& operator=(Arg&& arg) { B::operator=(std::forward(arg)); return *this; } // 2nd copy…
user1899020
  • 13,167
  • 21
  • 79
  • 154
1
vote
3 answers

Why GCC refuses a const reference within a copy-assignment operation?

I want to overload a common copy-assignment operator normally. At first I used a interface that only requires a const reference to the source, and explicitly disabled the interface that accepts a modifiable reference, but I can not pass the…
Leon
  • 1,489
  • 1
  • 12
  • 31
1
vote
1 answer

Is it really safe not to check in the copy assignemt operator whether an object is assigned to itself?

Here is an example implementing "Rule of Three" which I found: class Array { public: int size; int* vals; Array() : size(0), vals(NULL){} Array( int s, int* v ); Array(const Array&); // 1 Array&…
Maestro
  • 2,512
  • 9
  • 24
1
vote
1 answer

Can you create a new instance with the copy assignment operator?

I'm trying to wrap my head around the copy assignment operator in C++, and I'm wondering if there's a way to create a new instance if the object is null. class Person { public: Person(string name) { pName_ = new string(name); } ~Person() {…
DrHankPym
  • 23
  • 4
1
vote
1 answer

Gcc uses memcpy for implicit copy-assignment operator instead of member-wise copy

following the c++ 11 standard i suppose that g++ implicitly uses a member-wise copy whenever an assignment between two objects is done. Surprisingly I noticed that contrary to the standard, g++ seems to invoke a memcpy sized at the actual size of…
1
vote
1 answer

python assignment in array vs scalar

I have a 2D array A of shape (4,3), and a 1D array a of shape (4,). I want to swap the first two rows of A, as well as the first two elements in a. I did the following: A[0,:],A[1,:] = A[1,:],A[0,:] a[0],a[1] = a[1],a[0] Apparently, it works for a,…
Physicist
  • 2,848
  • 8
  • 33
  • 62
1
vote
0 answers

Copy-assignment in a custom vector

I am working on a custom vector (String_vector) based off of a custom string class (String). I am having trouble with my copy assignment, and I think I am in some sort of loop that doesn't terminate. In my implementation I return *this as shown…
iambicSam
  • 347
  • 1
  • 2
  • 11
1
vote
1 answer

Define Only Non-Trivial Copy Operations

I have a templated class which has many member variables. A small number of these variables have the template type of the class, the majority have fixed types. I'd like to copy between one instance of the class to another with conversions, but…
Richard
  • 56,349
  • 34
  • 180
  • 251
1
vote
1 answer

Are temporary lvalues a use case for std::move

I have a class like this: class Widget { public: Widget(A const& a) { /* do something */ } Widget(A&& a) { /* do something */ } }; Which I can use like this: A a{}; Widget widget{a}; If I came to the conclusion that I don't need a any…
hgiesel
  • 5,430
  • 2
  • 29
  • 56
1
vote
1 answer

Copiable mocks in Google Test Framework

Is it possible to make a mock class copiable in Google Test Framework? I've seen that the default copy constructor and copy assignment operator are deleted once that the MOCK_METHOD macros are used. Is there a way to workaround that?
nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
1
vote
1 answer

C++ copy costructor

Im trying to get a good grasp with copy constructors & I've found this part of code. #include using namespace std; class A1 { int data; public: A1(int i = 10) : data(i) { cout << "I…
Alex R.
  • 459
  • 5
  • 16
1
vote
4 answers

Is it a good practice to point to a new address on free store when dynamicly allocating?

Below is an exercise from C++ Primer 5th Edition: Exercise 13.22: Assume that we want HasPtr to behave like a value. That is, each object should have its own copy of the string to which the objects point. We’ll show the definitions of the…
Yue Wang
  • 1,710
  • 3
  • 18
  • 43
1
vote
1 answer

Why doesn't this vector assignment work?

Similar Questions: STL vector reserve() and copy() std::vector reserve() and push_back() is faster than resize() and array index, why? std::vector::resize() vs. std::vector::reserve() #include #include using namespace…
Yktula
  • 14,179
  • 14
  • 48
  • 71
1
vote
1 answer

C++11 - Copy construction of a smart pointer pointing to abstract type?

I like std::unique_ptr. It helps me out to prevent memory leaks, which is extremely useful. But there's one problem: copy assignment and construction is not allowed. Even though this restriction serves safety of a programmer, it is quite limiting,…
Helixirr
  • 911
  • 9
  • 18