Questions tagged [move-assignment-operator]

55 questions
1
vote
1 answer

Move constructor vs. Move assignment

As an extension to This question, i am trying to get my move assignment correct. I have the following code: // copy assignment operator LinkedList& operator= (LinkedList other) noexcept { swap(*this, other); return *this; } // move…
tannic
  • 37
  • 5
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…
1
vote
2 answers

Overloading "*" operator for a class to return class variable

I have two cpp files and one hpp file. Main.cpp, Ab.cpp and Ab.hpp. In these files I have created a class 'Ab' that has a default constructor and a constructor that takes a string. Within the class I want to redefine the * operator to set a given…
1
vote
1 answer

Is a self-assignment check really required while implementing move assignment operator for a class?

Type &Type::operator=(Type &&rhs) { if(this == &rhs) //is there any need of self-assignment . returh *this ; } ... } //since it will be called on r-value so why self-assignment ??
Hamza
  • 62
  • 7
1
vote
2 answers

Problem with move assignment in C++. Illegal instruction: 4

I’m writing a simple Matrix class, and I’ve defined, among others, an operator+ overloading and a move assignment. It looks like something happens when the two of them interact, but I can’t find where I am wrong. Here’s my code (I’ve deleted…
A.J.
  • 113
  • 1
  • 11
1
vote
1 answer

Inheritence of the move assignment operator in C++

I require some help in understanding the process of inheritance of move assignment operator. For a given base class class Base { public: /* Constructors and other utilities */ /* ... */ /* Default move assignment operator: */ Base…
1
vote
1 answer

C++ move assignment to uninitialized object?

As follow up to: Double free of child object after using the copy constructor I followed the rule of 5 as suggested. But now it seems like the move assignment is happening on an uninitialized object (to object id 0)? I expected it to move from…
eKKiM
  • 358
  • 2
  • 14
1
vote
1 answer

Assignment from rvalue allowed when assignment operator explicitly deleted?

Consider the following code, which compiles under Clang, GCC, and VS 2015 (online example): #include class S { public: S(int x) : i(x) { } ~S() { } S(S&&) = default; S(const S& ) = delete; S&…
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
  • 4,699
  • 1
  • 21
  • 47
1
vote
1 answer

Std::stringstream move assignment not working in gcc

An assignment like the following std::stringstream strstr; strstr = std::stringstream(someString) does give me an error when compiling in gcc: error: use of deleted function ‘std::basic_stringstream&…
bweber
  • 2,976
  • 2
  • 20
  • 49
1
vote
1 answer

What ctor/assignment operator should a vector class have?

I'm writing my own vector class (a container for x, y values) and I'm not really sure what constructors/assignment operators I should implement on my own and what I can count on the compiler to provide. Obviously, I need to write any method that…
0
votes
1 answer

class to class type conversion through constructor method output is 5500 why not 5555

Output is 5500, but why not 5555? class product { public: int b; }; class item { public: int a; item(product& obj) { cout << a; } item() {} void display() { cout << a; } }; int main() { item…
0
votes
1 answer

Move constructor and move assignment operator throw error when set to default in .cc source file

I have a template base class that looks roughly like this Vector.cc: template class Vector { private: T _data[D]; ... public: Vector(Vector&&); ... public: …
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

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…