assignment-operator

Why does valarray assignment not resize assignee per the documentation?

假装没事ソ 提交于 2020-01-02 04:27:26
问题 Code: #include <valarray> #include <iostream> using namespace std; int main() { valarray<int> v0(2, 4); valarray<int> v1; v1 = v0; cout << "v0.size: " << v0.size() << endl; cout << "v1.size: " << v1.size() << endl; cout << "v0[0]: " << v0[0] << endl; cout << "v1[0]: " << v1[0] << endl; } Output: v0.size: 4 v1.size: 0 v0[0]: 2 Segmentation fault For the assignment: v1 = v0; I would think the constructor: valarray<T>& operator=( const valarray<T>& other ); should be used and according to the

How to have the Xcode 3.1 compiler warn of assignment operator in an if statement?

纵饮孤独 提交于 2020-01-01 19:59:09
问题 I've tried searching the documentation and the internet as best as I'm able, but I haven't been able to get the Xcode compiler to issue a warning if the assignment operator is used in an if statement. I'm coming from RealBasic, where I've got an extremely strong habit of typing this sort of comparison: if x = 5 then ... In C, of course, that syntax assigns x the value of 5 then tests the result to see if it's nonzero, and the "correct" operator is: if (x == 5) { ... } I've found several

How can I initialize the default value of a CArray<CClass*> function parameter with an empty CArray?

南笙酒味 提交于 2019-12-31 07:04:03
问题 I know I could do this better with std::vector , but the application I am messing with, has already a bunch of CArray parameters on a lot of related functions ... and I will not change them all at the moment! I simply want to define an empty CArray<CClass*> — array of pointers to CClass , so the problem can not be on the CClass constructor — as the default value of a function parameter. Approach 1 If I try to do it with assignment operator and default constructor : void Function(CArray<CClass

Assignment operator - Self-assignment

China☆狼群 提交于 2019-12-30 08:00:19
问题 Does the compiler generated assignment operator guard against self assignment? class T { int x; public: T(int X = 0): x(X) {} }; int main() { T a(1); a = a; } Do I always need to protect against self-assignment even when the class members aren't of pointer type? 回答1: Does the compiler generated assignment operator guard against self assignment? No, it does not. It merely performs a member-wise copy, where each member is copied by its own assignment operator (which may also be programmer

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

非 Y 不嫁゛ 提交于 2019-12-29 04:28:09
问题 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

What is wrong with “checking for self-assignment” and what does it mean?

假装没事ソ 提交于 2019-12-29 03:14:30
问题 In Herb Sutter's book Exceptional C++ (1999) , he has words in item 10's solution: "Exception-unsafe" and "poor design" go hand in hand. If a piece of code isn't exception-safe, that's generally okay and can simply be fixed. But if a piece of code cannot be made exception-safe because of its underlying design, that almost always is a signal of its poor design. Example 1: A function with two different responsibilities is difficult to make exception-safe. Example 2: A copy assignment operator

What is wrong with “checking for self-assignment” and what does it mean?

↘锁芯ラ 提交于 2019-12-29 03:14:08
问题 In Herb Sutter's book Exceptional C++ (1999) , he has words in item 10's solution: "Exception-unsafe" and "poor design" go hand in hand. If a piece of code isn't exception-safe, that's generally okay and can simply be fixed. But if a piece of code cannot be made exception-safe because of its underlying design, that almost always is a signal of its poor design. Example 1: A function with two different responsibilities is difficult to make exception-safe. Example 2: A copy assignment operator

Placement new and assignment of class with const member

做~自己de王妃 提交于 2019-12-28 00:55:33
问题 Why is that undefined behaviour? struct s { const int id; // <-- const member s(int id): id(id) {} s& operator =(const s& m) { return *new(this) s(m); // <-- undefined behavior? } }; (Quote from the standard would be nice). This question arose from this answer. 回答1: There is nothing that makes the shown code snippet inherently UB. However, it is almost certain UB will follow immediately under any normal usage. From [basic.life]/8 (emphasis mine) If, after the lifetime of an object has ended

Update subset of data.table based on join

余生长醉 提交于 2019-12-27 14:54:28
问题 I have two data tables, DT1 and DT2: set.seed(1) DT1<-data.table(id1=rep(1:3,2),id2=sample(letters,6), v1=rnorm(6), key="id2") DT1 ## id1 id2 v1 ## 1: 2 e 0.7383247 ## 2: 1 g 1.5952808 ## 3: 2 j 0.3295078 ## 4: 3 n -0.8204684 ## 5: 3 s 0.5757814 ## 6: 1 u 0.4874291 DT2<-data.table(id2=c("n","u"), v1=0, key="id2") DT2 ## id2 v1 ## 1: n 0 ## 2: u 0 I would like to update DT1 based on a join with DT2, but only for a subset of DT1. For example, for DT1[id1==3] , I would expect the value of v1 in

Shortcut “or-assignment” (|=) operator in Java

拟墨画扇 提交于 2019-12-27 11:04:32
问题 I have a long set of comparisons to do in Java, and I'd like to know if one or more of them come out as true. The string of comparisons was long and difficult to read, so I broke it up for readability, and automatically went to use a shortcut operator |= rather than negativeValue = negativeValue || boolean . boolean negativeValue = false; negativeValue |= (defaultStock < 0); negativeValue |= (defaultWholesale < 0); negativeValue |= (defaultRetail < 0); negativeValue |= (defaultDelivery < 0);