assignment-operator

Reducing code duplication between operator= and the copy constructor

与世无争的帅哥 提交于 2019-11-28 21:25:21
I have a class that requires a non-default copy constructor and assignment operator (it contains lists of pointers). Is there any general way to reduce the code duplication between the copy constructor and the assignment operator? There's no "general way" for writing custom copy constructors and assignment operators that works in all cases. But there's an idiom called "copy-&-swap": class myclass { ... public: myclass(myclass const&); void swap(myclass & with); myclass& operator=(myclass copy) { this->swap(copy); return *this; } ... }; It's useful in many (but not all) situations. Sometimes

Array type char[] is not assignable [duplicate]

落花浮王杯 提交于 2019-11-28 20:27:55
This question already has an answer here: problems with char array = char array 2 answers Well here is my first post. I've been trying to do this choice choosing thing and I want the user to choose only numbers instead of typing them down (easier) but when I want the numbers to equal a string, it says "array type char[30] is not assignable". Even if at the back I put semi-colon or not. #include <stdio.h> int main() { int choice1; char word[30]; printf("You have three choice.\n"); printf("[1] Jump [2] Run [3] Dance\n"); scanf("%d",&choice1); if (choice1 == 1) { word = "Jump" //Error #1 } else

Checklist for writing copy constructor and assignment operator in C++

六月ゝ 毕业季﹏ 提交于 2019-11-28 19:04:46
问题 Please write a list of tasks that a copy constructor and assignment operator need to do in C++ to keep exception safety, avoid memory leaks etc. 回答1: First be sure you really need to support copy. Most of the time it is not the case, and thus disabling both is the way to go. Sometimes, you'll still need to provide duplication on a class from a polymorphic hierarchy, in that case: disable the assignment operator, write a (protected?) copy constructor, and provide a virtual clone() function.

C++ linked List assignment Operator

狂风中的少年 提交于 2019-11-28 14:02:38
问题 Trying to build an assignment Operator for a single linked list class. I thought I built it correctly, but am still getting a memory leak. The class consists of a a First and Last variable. And then a Node structure. The Node structure looks like this: struct node { TYPE value; node * next; node * last; }; My Assignment Operator looks like this, it still has a memory leak queue& queue::operator=(const queue &rhs){ if(this == &rhs ){ node *ptr = first; node *temp; while(ptr != NULL){ temp =

Does it improve safety to mark assignment operators as lvalue-only?

蹲街弑〆低调 提交于 2019-11-28 12:03:25
If T is a class type with the default signature for assignment operator, then we can write: T const &ref = ( T{} = something ); which creates a dangling reference. However, with the signature: T &operator=(T t) & the above code with dangling reference will fail to compile. This would prevent some situations where we return an lvalue that designates a temporary object -- undesirable situations because they can lead to dangling references. Is there any reason not to do this; would we be disabling any valid use cases for the assignment operators? I think the same comments can apply to the

Is a += b more efficient than a = a + b in C?

岁酱吖の 提交于 2019-11-28 10:02:26
I know in some languages the following: a += b is more efficient than: a = a + b because it removes the need for creating a temporary variable. Is this the case in C? Is it more efficient to use += (and, therefore also -= *= etc) So here's a definitive answer... $ cat junk1.c #include <stdio.h> int main() { long a, s = 0; for (a = 0; a < 1000000000; a++) { s = s + a * a; } printf("Final sum: %ld\n", s); } michael@isolde:~/junk$ cat junk2.c #include <stdio.h> int main() { long a, s = 0; for (a = 0; a < 1000000000; a++) { s += a * a; } printf("Final sum: %ld\n", s); } michael@isolde:~/junk$ for

Assigning a vector of one type to a vector of another type

好久不见. 提交于 2019-11-28 07:51:12
问题 I have an "Event" class. Due to the way dates are handled, we need to wrap this class in a "UIEvent" class, which holds the Event, and the date of the Event in another format. What is the best way of allowing conversion from Event to UIEvent and back? I thought overloading the assignment or copy constructor of UIEvent to accept Events (and vice versa)might be best. 回答1: There are two simple options that I can think of. The first option would be the one you describe: create a constructor that

PHP's =& operator

拈花ヽ惹草 提交于 2019-11-28 05:14:00
Are both these PHP statements doing the same thing?: $o =& $thing; $o = &$thing; judda Yes, they are both the exact same thing. They just take the reference of the object and reference it within the variable $o . Please note, thing should be variables. They're not the same thing, syntactically speaking. The operator is the atomic =& and this actually matters. For instance you can't use the =& operator in a ternary expression. Neither of the following are valid syntax: $f = isset($field[0]) ? &$field[0] : &$field; $f =& isset($field[0]) ? $field[0] : $field; So instead you would use this: isset

What is the R assignment operator := for?

这一生的挚爱 提交于 2019-11-28 02:36:38
问题 By digging into R source code (file R-3.2.2/src/main/gram.y lines 2836 to 2852) I found that the R parser/tokenizer considers that := is a LEFT_ASSIGNMENT token. But when trying to use it as an assignment operator in R.3.2.2 , I have an error (impossible to find function for := ...) but as you can see R considers it as an assignment like <- : > myVar := 42 Erreur : impossible de trouver la fonction ":=" > := Erreur : unexpected assignment in ":=" > <- Erreur : unexpected assignment in "<-" Is

The assignment operator and initialization

给你一囗甜甜゛ 提交于 2019-11-28 02:16:15
I'm studying the C++ programming language and I'm reading the chapter about assignment operator ( = ). In C++ initalization and assignment are operation so similar that we can use the same notation. But my question is : when i initialize a variable am I doing it with the assignment operator ? When i assign to a variable, am I doing it with the assignment operator ? I think that the only difference is between initialization and assignment because when we initalize a variable we are giving it a new value with the assignmnet operator, when we assign to a variable we are replacing the old value of