assignment-operator

How to utilize template copy&move constructor and assignment operator?

拜拜、爱过 提交于 2020-02-03 07:54:13
问题 Consider the following C++ code with my failed attempt to avoid preference of non-template copy&move constructors and assignment operators: template<typename T> class A { public: A() { /* implementation here */ } // Remove from the overloads the default copy&move constructors and assignment operators A(const A&) = delete; A& operator=(const A&) = delete; A(A&&) = delete; A& operator=(A&&) = delete; // I want these to be used e.g. by std::vector template<typename U> A(const A<U>& fellow) { /*

can memcpy() be used to change “const” member data?

核能气质少年 提交于 2020-02-02 03:30:07
问题 For a struct with const members struct point { const int x; const int y; }; that is used as member data struct Foo { point pt{ 0, 0 }; void move_x(int value); }; How can Foo::move_x() be written to update Foo::pt ? Is it OK to use memcpy() ? #include <memory.h> void Foo::move_x(int value) { const point pt_{ pt.x + value, pt.y }; (void) memcpy(&pt, &pt_, sizeof(pt_)); // pt = pt_; } This can be safely done using a pointer #include <memory> struct Bar { std::unique_ptr<point> pPt_{ new point{ 0

Preventing copy construction and assignment of a return value reference

你说的曾经没有我的故事 提交于 2020-01-24 05:44:06
问题 If I have a function that returns a reference to an instance of a class that I don't have control over its source, say list<int> : list<int>& f(); I want to ensure that its value is only assigned to another reference, e.g.: list<int> &a_list = f(); If the user were instead to do: list<int> a_list = f(); // note: no '&', so the list is copied I want it to be a compile-time error since the user would be manipulating only a copy of the list and not the original list (which is never what is

Do derived classes indirectly inherit base's assignment operator?

孤街浪徒 提交于 2020-01-23 02:42:15
问题 I'm trying to understand this behaviour but it seems I don't. Please see this code: #include <iostream> using namespace std; class Base { public: void operator=(const Base& rf) { cout << "base operator=" << endl; this->y = rf.y; } int y; Base() : y(100) { } }; class Derived : public Base { public: int x; Derived() : x(100) { } }; int main() { Derived test; Derived test2; test2.x = 0; test2.y = 0; test.operator=(test2); // operator auto-generated for derived class but... cout << test.x << endl

What's the point of evaluating left operand of assignment operator in C?

一个人想着一个人 提交于 2020-01-12 06:47:07
问题 According to ISO C11 - 6.5.16.3, it says that An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment, but is not an lvalue. The type of an assignment expression is the type the left operand would have after lvalue conversion. The side effect of updating the stored value of the left operand is sequenced after the value computations of the left and right operands. The evaluations of the

const member and assignment operator. How to avoid the undefined behavior?

喜欢而已 提交于 2020-01-12 03:58:05
问题 I answered the question about std::vector of objects and const-correctness, and received a comment about undefined behavior. I do not agree and therefore I have a question. Consider the class with const member: class A { public: const int c; // must not be modified! A(int c) : c(c) {} A(const A& copy) : c(copy.c) { } // No assignment operator }; I want to have an assignment operator but I do not want to use const_cast like in the following code from one of the answers: A& operator=(const A&

When does the compiler provide definitions for the special members of a class?

拟墨画扇 提交于 2020-01-11 03:18:06
问题 I know that when I define an empty class and provide no declarations at all, the compiler will provide definitions for the default and copy constructor, destructor and copy assignment operator. What are the rules for that? When does the compiler not provide a, say, copy constructor? What about the move constructor and move assignment operator? (Example: The compiler will not provide definitions for any assignment operator if my class has a reference member like int& . When else will something

Evaluation order between a method call and arguments in Java

梦想的初衷 提交于 2020-01-06 05:31:08
问题 Dealing with another SO question, I was wondering if the code below has an undefined behavior: if (str.equals(str = getAnotherString())) { // [...] } I tend to think the str reference from which the equals() call is made is evaluated before the further str assignment passed as argument. Is there a source about it? 回答1: This is clearly specified in the JLS Section 15.12.4: At run time, method invocation requires five steps. First, a target reference may be computed. Second, the argument

Evaluation order between a method call and arguments in Java

房东的猫 提交于 2020-01-06 05:31:03
问题 Dealing with another SO question, I was wondering if the code below has an undefined behavior: if (str.equals(str = getAnotherString())) { // [...] } I tend to think the str reference from which the equals() call is made is evaluated before the further str assignment passed as argument. Is there a source about it? 回答1: This is clearly specified in the JLS Section 15.12.4: At run time, method invocation requires five steps. First, a target reference may be computed. Second, the argument

Javascript: How can I mix in methods of another Object B to my Object A without copying but with linking?

早过忘川 提交于 2020-01-05 09:46:41
问题 If I create an Object A: let A = {}; And want to mix in methods from another Object B: let B = { foo() { alert("Boo!"); } }; Normally I would call: Object.assign(A, B); Then I change my function foo: Object.assign(B, { foo() { alert("Hooray!"); } }); After that I call foo: A.foo(); // Actual output: "Boo!" But I want that output to be "Hooray!". So far I found out, that Object.assign only copies methods in the target, but it doesn't link them. About inheritance and composition I found useful