variable-assignment

Why do object assignments refer to memory locations when primitive types don't? [duplicate]

蓝咒 提交于 2019-12-02 10:07:42
This question already has an answer here: Is Java “pass-by-reference” or “pass-by-value”? 84 answers I have a bit of an odd question regarding how primitive types work in Java. When using Objects when you assign an ObjectA to be ObjectB be like such Rectangle ObjectB = new Rectangle(); ObjectA = ObjectB; Any calls to ObjectA refer now to ObjectB's memory location. However when using integers or other primitive types this is not the case. For example int x = 3; int y = x; int x = 5; return y; y will return 3, the value of x when y was initialized. The question I have is why does assignment for

Assign Many Variables at Once, Python

随声附和 提交于 2019-12-02 09:39:44
问题 Is there a better way to do this? a, b, c, = "yyy", "yyy", "yyy" Obvious attempts fails a, b, c, = "yyy" a, b, c = "yyy"*3 Technically, the following works, but I don't think it's intuitive as this logic says a, b, and c are the same, whereas all I'm trying to do is say they initialize as the same value a=b=c="yyy" 回答1: No need to use tuple assignment here; the right-hand value is immutable, so you can just as well share the reference: a = b = c = 'yyy' This is not unintuitive at all, in my

Overloading assignment operator for pointers to two different classes

荒凉一梦 提交于 2019-12-02 09:19:40
My Question: I'm trying to overload the assignment operator for pointers to two different classes. Here is an example: dc.h: #ifndef DC_H_ #define DC_H_ #include "ic.h" class dc { double d; char c; public: dc(): d(0), c(0) { } dc(double d_, char c_): d(d_), c(c_) { } dc* operator=(const ic* rhs); ~dc() { } }; #endif /* DC_H_ */ class ic.h: #ifndef IC_H_ #define IC_H_ class ic { int i; char c; public: ic(): i(0), c(0) { } ic(int i_, char c_): i(i_), c(c_) { } ~ic() { } }; #endif /* IC_H_ */ dc.cpp: #include "dc.h" dc* dc::operator=(const ic* rhs) { d = rhs->i; c = rhs->c; return this; } ic.cpp:

PHP Logical Operators precedence affects variable assignment results strangely

北慕城南 提交于 2019-12-02 09:08:30
$var4 = 123; function fn1($p1) { return array('p1' => 1, 'p2' => 2); } if ($var1 = fn1(1) AND $var4 == 123) { print_r($var1); } if ($var2 = fn1(1) && $var4 == 123) { print_r($var2); } if (($var3 = fn1(1)) && $var4 == 123) { print_r($var3); } If you run this simple script it will output strange results, at least for me!! First output from first if expression will result in an array returned from the function & assigned to the $var1 variable, which is what I'm expecting, well? Second output from second if expression will result in an integer '1' assigned to the $var2 variable, which is NOT

Form validation stops working when I render pages using another controller

一笑奈何 提交于 2019-12-02 08:58:27
问题 I wrote a quick CI library class to render my pages so I wouldn't have to keep typing '$this->load->view' all the time and for DRY. Now when I re-render my contact form after passing in invalid data the error messages aren't showing up. The library class: class Page extends CI_Controller { public function render($page, $data) { // $page should be path to page view $this->load->view('fragments/header', $data); $this->load->view('fragments/navigation'); $this->load->view($page); $this->load-

How can the assignment from int to object be possible in C++?

蓝咒 提交于 2019-12-02 08:57:01
class phone { public: phone(int x) { num = x; } int number(void) { return num; } void number(int x) { num = x; } private: int num; }; int main(void) { phone p1(10); p1 = 20; // here! return 0; } Hi, guys Just I declared a simple class like above one. After that I assigned int value to the object that class, then it worked! (I printed its value. It was stored properly) If there is not a construct with int parameter, a compile error occurred. So, I think it's related with a constructor. Is that right? Please give me a good explanation. Thanks. This is legal because C++ interprets any constructor

Passing common title to all view and models CodeIgniter

你。 提交于 2019-12-02 08:18:34
I have this controller <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Main extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper('url'); $this->load->helper('text'); } public function index() { $this->home(); } public function home() { $data['title']="Somesite"; $this->load->view("view_home", $data); } public function blog() { $data['title']="Somesite"; $this->load->view("view_blog", $data); } public function answers() { $data['title']="Somesite"; $this->load->view("view_answers", $data); } } As you may see $data['title']

Cannot assign value to global array in c++

假装没事ソ 提交于 2019-12-02 07:48:05
I've got this code: #include <iostream> int tabela[1]; tabela[0] = 1; int main(){ std::cout << tabela[0]; std::cin.get(); return 0; } and it doesn't want to work. My compiler says " "tabela" doesn't name a type". However if I do this: #include <iostream> int tabela[1]; int main(){ tabela[0] = 1; std::cout << tabela[0]; std::cin.get(); return 0; } It works. Can sb explain me why? Thanks in advance. At the outermost level, a C++ file is a sequence of declarations. tabela[0] = 1; is not a declaration - it's a statement (in particular an expression-statement). A function body, however, is a

python assignment in array vs scalar

こ雲淡風輕ζ 提交于 2019-12-02 07:11:52
问题 I have a 2D array A of shape (4,3) , and a 1D array a of shape (4,) . I want to swap the first two rows of A , as well as the first two elements in a . I did the following: A[0,:],A[1,:] = A[1,:],A[0,:] a[0],a[1] = a[1],a[0] Apparently, it works for a , but fails for A . Now, the second row becomes the first row, but the first row remains unchanged. If I do the following: first_row_copy = A[0,:].copy() A[0,:] = A[1,:] A[1,:] = first_row_copy Then, it seems to work. Why the first method doesn

composite inheritance: how to assign a final field at sub-class constructor which depends on 'this' value (backward reference)?

≡放荡痞女 提交于 2019-12-02 07:06:20
I use composite classes to group functionalities. But, the class A (with composite A1), got inherited by B (with composite B1), and a behavior existent at A1 is going to be adapted at B1, but the final a1 must be a B1 instance for this to work. Obs.: I have ways to make sure the composite instantiation happens properly (only by its composite partner). Unable to assign a B1 object to a1 final field: class finalFieldTestFails{ class A1{ A1(A a){} } class A{ protected final A1 a1; A(){ this.a1 = new A1(this); } A(A1 a1){ this.a1 = a1; } } class B1 extends A1{ B1(B b){ super(b); } } class B