assignment-operator

Assignment operator and copy constructor in the presence of references

亡梦爱人 提交于 2019-11-29 09:45:20
I am just experimenting with the references using this code: class A { }; class B { public: B(A& a): m_a(a){} A& m_a; }; int main() { A a; B b(a); B b1 = b; } I was expecting both B b1 = b; to produce a error. Instead when I compile with VS2008 I just get a warning warning C4512: 'B' : assignment operator could not be generated I understand why I am getting this warning. But shouldn't the compiler generating an error for the B b1 = b; statement too? It is like it generated copy constructor but didn't generate assignment operator. Aren't these two inherently linked to one another ? does it

assignment of class with const member

眉间皱痕 提交于 2019-11-29 09:45:15
Consider the following code: struct s { const int id; s(int _id): id(_id) {} }; // ... vector<s> v; v.push_back(s(1)); I get a compiler error that 'const int id' cannot use default assignment operator. Q1. Why does push_back() need an assignment operator? A1. Because the current c++ standard says so. Q2. What should I do? I don't want to give up the const specifier I want the data to be copied A2. I will use smart pointers. Q3. I came up with a "solution", which seems rather insane: s& operator =(const s& m) { if(this == &m) return *this; this->~s(); return *new(this) s(m); } Should I avoid

What is the R assignment operator := for?

梦想与她 提交于 2019-11-29 09:11:46
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 it a bug, or does the token := need to be removed from the tokenizer source code? Is there a past

compiler generated constructors [duplicate]

*爱你&永不变心* 提交于 2019-11-29 06:16:14
This question already has an answer here: Conditions for automatic generation of default/copy/move ctor and copy/move assignment operator? 3 answers This is just a quick question to understand correctly what happens when you create a class with a constructor like this: class A { public: A() {} }; I know that no default constructor is generated since it is already defined but are copy and assignment constructors generated by the compiler or in other words do i need to declare a private copy constructor and a private assignment operator in order to prevent this from happening? class A { private:

Low level details of C/C++ assignment operator implementation. What does it return?

大憨熊 提交于 2019-11-29 04:19:09
I m a total newbie to a C++ world (and C too). And don't know all its details. But one thing really bothers me. It is constructions like : while (a=b) {...} .As I understand this magic works because assignment operator in C and C++ returns something. So the questions: what does it return? Is this a documented thing?Does it work the same in C and C++. Low level details about assignment operator and its implementation in both C and C++ (if there is a difference) will be very appreciated! I hope that this question won't be closed, because I can't find a comprehensive explanation and good material

Get name of x when defining `(<-` operator

[亡魂溺海] 提交于 2019-11-29 02:37:19
I want to define (<- and access the name of the left hand side argument : *<- functions use internally an intermediate '*tmp*' variable. Is it still possible to get the name of x ? `(<-` <- function(x,value){ print(deparse(substitute(value))) print(deparse(substitute(x))) print(match.call()) value } foo <- 0 (foo) <- 3 # [1] "3" # [1] "*tmp*" # `(<-`(x = `*tmp*`, value = 3)# [1] "3" I want to get "foo" from inside the function. I tried to hack it by using tracemem , i.e. calling sapply(ls(envir = parent.frame()),tracemem) and tracemem(x) inside of the functions but the address of foo , *temp*

What is the Rule of Four (and a half)?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 02:31:32
问题 For properly handling object copying, the rule of thumb is the Rule of Three. With C++11, move semantics are a thing, so instead it's the Rule of Five. However, in discussions around here and on the internet, I've also seen references to the Rule of Four (and a half), which is a combination of the Rule of Five and the copy-and-swap idiom. So what exactly is the Rule of Four (and a half)? Which functions need to be implemented, and what should each function's body look like? Which function is

C++ function returns a rvalue, but that can be assigned a new value?

拟墨画扇 提交于 2019-11-29 02:13:40
The code is as follows: #include <iostream> using namespace std; class A { }; A rtByValue() { return A(); } void passByRef(A &aRef) { // do nothing } int main() { A aa; rtByValue() = aa; // compile without errors passByRef(rtByValue()); // compile with error return 0; } The g++ compiler gives the following error: d.cpp: In function ‘int main()’: d.cpp:19:23: error: invalid initialization of non-const reference of type ‘A&’ from an rvalue of type ‘A’ d.cpp:12:6: error: in passing argument 1 of ‘void passByRef(A&)’ It says that I can't pass an rvalue as an argument of a non-const reference, but

Python: yield and yield assignment

拥有回忆 提交于 2019-11-29 00:55:21
问题 How does this code, involving assignment and the yield operator, work? The results are rather confounding. def test1(x): for i in x: _ = yield i yield _ def test2(x): for i in x: _ = yield i r1 = test1([1,2,3]) r2 = test2([1,2,3]) print list(r1) print list(r2) Output: [1, None, 2, None, 3, None] [1, 2, 3] 回答1: The assignment syntax ("yield expression") allows you to treat the generator as a rudimentary coroutine. First proposed in PEP 342 and documented here: https://docs.python.org/2

Lua operators, why isn't +=, -= and so on defined?

纵然是瞬间 提交于 2019-11-28 22:25:37
This is a question I've been mildly irritated about for some time and just never got around to search the answer to. However I thought I might at least ask the question and perhaps someone can explain. Basically many languages I've worked in utilize syntactic sugar to write (using syntax from C++): int main() { int a = 2; a += 3; // a=a+3 } while in lua the += is not defined, so I would have to write a=a+3 , which again is all about syntactical sugar. when using a more "meaningful" variable name such as: bleed_damage_over_time or something it starts getting tedious to write: bleed_damage_over