assignment-operator

Initialization different from assignment?

安稳与你 提交于 2019-12-07 12:31:54
问题 1.char str[] = "hello"; //legal 2.char str1[]; str1 = "hello"; // illegal I understand that "hello" returns the address of the string literal from the string literal pool which cannot be directly assigned to an array variable. And in the first case the characters from the "hello" literal are copied one by one into the array with a '\0' added at the end. Is this because the assignment operator "=" is overloaded here to support this? I would also like to know other interesting cases wherein

C++17 sequencing in assignment: still not implemented in GCC?

╄→尐↘猪︶ㄣ 提交于 2019-12-07 08:01:01
问题 I tried the following code as a naive attempt to implement swapping of R and B bytes in an ABGR word #include <stdio.h> #include <stdint.h> uint32_t ABGR_to_ARGB(uint32_t abgr) { return ((abgr ^= (abgr >> 16) & 0xFF) ^= (abgr & 0xFF) << 16) ^= (abgr >> 16) & 0xFF; } int main() { uint32_t tmp = 0x11223344; printf("%x %x\n", tmp, ABGR_to_ARGB(tmp)); } To my surprise this code "worked" in GCC in C++17 mode - the bytes were swapped http://coliru.stacked-crooked.com/a/43d0fc47f5539746 But it is

In the expression left() = right(), why is right() sequenced first?

会有一股神秘感。 提交于 2019-12-06 19:12:03
问题 In C++, the expression left() = right() evaluates right() left() in that sequence. The right() goes first, as has been discussed here. I cannot think of a reason for right() to go first. Can you? I presume that there exists a reason. Otherwise, the standard would hardly say what it says, but consider: right() will return some result. At the machine-code level, does the CPU not need to know where to put the result right() will return before asking right() to return it? If you happen to know

What does Swift's optional binding do to the type it's arguments?

纵饮孤独 提交于 2019-12-06 16:27:09
问题 Why is if let y: Int? = nil { ... } the same as if let y: Int? = nil as Int?? { ... } (and thus an invalid assignment) especially when, on its own let y: Int? = nil is not the same as let y: Int? = nil as Int?? (since let y: Int? = nil is a valid assignment)? 回答1: OK, I will answer, with my poor English skills ;-) Let's start with this: if let lvalue:T = rvalue { ... } At first the compiler tries to convert rvalue to T? by wrapping with Optional . For example: typealias T = Int let rvalue:Int

Is there a way to customize/override assignment operations in JavasScript?

為{幸葍}努か 提交于 2019-12-06 16:08:46
问题 Every time I assign a string , I'd actually like to assign a string object , without the extra code. This var foo = "bar"; becomes var foo = new String("bar"); Basically hi-jacking the assignment. Follow-up: If the above is not possible is there a way to prototype the string variable type, rather than the String object? As pointed out by armando, the foo would be a string type, but is essentially a customized array. It would be nice to be able to prototype functions to that class. 回答1: No

How does an equal to expression work in a printf placeholder? [duplicate]

只谈情不闲聊 提交于 2019-12-06 15:13:37
This question already has answers here : Why are these constructs using pre and post-increment undefined behavior? (14 answers) Closed last year . I have the following code snippet: main( ) { int k = 35 ; printf ( "\n%d %d %d", k == 35, k = 50, k > 40 ) ; } which produces the following output 0 50 0 I'm not sure I understand how the first value of the printf comes to 0 . When the value of k is compared with 35 , it should ideally return (and thus print) 1, but how is it printing zero? The other two values that are produced- 50 and 0 are all right, because in the second value, the value of k is

Operators precedence of “or” and assignment

故事扮演 提交于 2019-12-06 10:39:37
Found some interesting code snippet today. Simplified, it looks like this: $var = null; $var or $var = '123'; $var or $var = '312'; var_dump($var); The thing is that, as i know, precedence of assignment is higher that OR , so, as i assume, var_dump should output 312 (first - assign, second - compare logically). But result is defferent, i getting 123 (first - check if $var converting to true , second - if not, assign value). The questions is how does it work? Why behavior is the same for or and || ? You can see examples about this behaviour in Logical Operators Also you can read artical about

Virtual assignment operator overloading- how the correct overloaded function is chosen?

a 夏天 提交于 2019-12-06 04:49:23
The following code (from C++ FAQs 24.11) is implementing virtual assignment operator overloading and overriding: #include <iostream> using namespace std; class B{ public: virtual ~B() throw(); virtual B& operator= (const B& b) throw(); }; B::~B() throw(){} B& B::operator= (const B& b) throw() { cout << "B::operator=(const B&)\n"; return *this; } class D : public B{ public: virtual D& operator= (const B& b) throw(); D& operator= (const D& d) throw(); }; D& D::operator= (const B& b) throw() {cout << "D::operator= (const B&)\n"; return *this;} D& D::operator= (const D& d) throw() {cout << "D:

Constant class members, assignment operator and QList

北城余情 提交于 2019-12-06 03:38:36
问题 Please conform if I am correct and tell me whether there is a better solution: I understand that objects with constant members like int const width; can not be handled by the synthetic assignment operator that is implicitly created by the compiler. But QList (and I suppose std::list, too) needs a working assignment operator. So when I want to use objects with constant members and QList I have three possibilities: Don't use constant members. (Not a solution) Implement my own assignment

Substitute a vector value with two values in MATLAB

孤街浪徒 提交于 2019-12-06 03:00:57
问题 I have to create a function that takes as input a vector v and three scalars a , b and c . The function replaces every element of v that is equal to a with a two element array [b,c] . For example, given v = [1,2,3,4] and a = 2, b = 5, c = 5 , the output would be: out = [1,5,5,3,4] My first attempt was to try this: v = [1,2,3,4]; v(2) = [5,5]; However, I get an error, so I do not understand how to put two values in the place of one in a vector, i.e. shift all the following values one position