assignment-operator

Return value of assignment operation in Java

旧巷老猫 提交于 2019-11-26 03:59:33
问题 I encountered a statement in Java while ((line = reader.readLine()) != null) { out.append(line); } How do assignment operations return a value in Java? The statement we are checking is line = reader.readLine() and we compare it with null . Since readLine will return a string, how exactly are we checking for null ? 回答1: The assignment operator in Java returns the assigned value (like it does, e.g., in c). So here, readLine() will be executed, and it's return value stored in line . That value

How to use base class's constructors and assignment operator in C++?

我们两清 提交于 2019-11-26 03:37:22
问题 I have a class B with a set of constructors and an assignment operator. Here it is: class B { public: B(); B(const string& s); B(const B& b) { (*this) = b; } B& operator=(const B & b); private: virtual void foo(); // and other private member variables and functions }; I want to create an inheriting class D that will just override the function foo() , and no other change is required. But, I want D to have the same set of constructors, including copy constructor and assignment operator as B : D

“Pointer from integer/integer from pointer without a cast” issues

夙愿已清 提交于 2019-11-26 02:58:52
问题 This question is meant to be a FAQ entry for all initialization/assignment between integer and pointer issues. I want to do write code where a pointer is set to a specific memory address, for example 0x12345678 . But when compiling this code with the gcc compiler, I get \"initialization makes pointer from integer without a cast\" warnings/errors: int* p = 0x12345678; Similarly, this code gives \"initialization makes integer from pointer without a cast\": int* p = ...; int i = p; If I do the

Is it possible to overload Python assignment?

 ̄綄美尐妖づ 提交于 2019-11-26 02:37:34
问题 Is there a magic method that can overload the assignment operator, like __assign__(self, new_value) ? I\'d like to forbid a re-bind for an instance: class Protect(): def __assign__(self, value): raise Exception(\"This is an ex-parrot\") var = Protect() # once assigned... var = 1 # this should raise Exception() Is it possible? Is it insane? Should I be on medicine? 回答1: The way you describe it is absolutely not possible. Assignment to a name is a fundamental feature of Python and no hooks have

What is the motivation for Scala assignment evaluating to Unit rather than the value assigned?

守給你的承諾、 提交于 2019-11-26 02:09:08
问题 What is the motivation for Scala assignment evaluating to Unit rather than the value assigned? A common pattern in I/O programming is to do things like this: while ((bytesRead = in.read(buffer)) != -1) { ... But this is not possible in Scala because... bytesRead = in.read(buffer) .. returns Unit, not the new value of bytesRead. Seems like an interesting thing to leave out of a functional language. I am wondering why it was done so? 回答1: I advocated for having assignments return the value

Why must the copy assignment operator return a reference/const reference?

半腔热情 提交于 2019-11-26 00:41:53
问题 In C++, the concept of returning reference from the copy assignment operator is unclear to me. Why can\'t the copy assignment operator return a copy of the new object? In addition, if I have class A , and the following: A a1(param); A a2 = a1; A a3; a3 = a2; //<--- this is the problematic line The operator= is defined as follows: A A::operator=(const A& a) { if (this == &a) { return *this; } param = a.param; return *this; } 回答1: Strictly speaking, the result of a copy assignment operator

data.table objects assigned with := from within function not printed

末鹿安然 提交于 2019-11-26 00:18:34
问题 I would like to modify a data.table within a function. If I use the := feature within the function, the result is only printed for the second call. Look at the following illustration: library(data.table) mydt <- data.table(x = 1:3, y = 5:7) myfunction <- function(dt) { dt[, z := y - x] dt } When I call only the function, the table is not printed (which is the standard behaviour. However, if I save the returned data.table into a new object, it is not printed at the first call, only for the

The forgotten assignment operator “=” and the commonplace “:=”

老子叫甜甜 提交于 2019-11-26 00:15:50
问题 The documentation for PL/pgSQL says, that declaration and assignment to variables is done with := . But a simple, shorter and more modern (see footnote) = seems to work as expected: CREATE OR REPLACE FUNCTION foo() RETURNS int AS $$ DECLARE i int; BEGIN i = 0; WHILE NOT i = 25 LOOP i = i + 1; i = i * i; END LOOP; RETURN i; END; $$ LANGUAGE plpgsql; > SELECT foo(); 25 Please note, that Pl/pgSQL can distinguish assignment and comparison clearly as shown in the line WHILE NOT i = 25 LOOP So, the

What is The Rule of Three?

99封情书 提交于 2019-11-25 22:50:37
问题 What does copying an object mean? What are the copy constructor and the copy assignment operator ? When do I need to declare them myself? How can I prevent my objects from being copied? 回答1: Introduction C++ treats variables of user-defined types with value semantics . This means that objects are implicitly copied in various contexts, and we should understand what "copying an object" actually means. Let us consider a simple example: class person { std::string name; int age; public: person

Multiple assignment and evaluation order in Python

北城余情 提交于 2019-11-25 22:31:34
问题 What is the difference between the following Python expressions: # First: x,y = y,x+y # Second: x = y y = x+y First gives different results than Second . e.g., First: >>> x = 1 >>> y = 2 >>> x,y = y,x+y >>> x 2 >>> y 3 Second: >>> x = 1 >>> y = 2 >>> x = y >>> y = x+y >>> x 2 >>> y 4 y is 3 in First and 4 in Second 回答1: In an assignment statement, the right-hand side is always evaluated fully before doing the actual setting of variables. So, x, y = y, x + y evaluates y (let's call the result