pass-by-value

how do people deal with iterating a Swift struct value-type property?

流过昼夜 提交于 2019-12-03 03:22:57
Here's an obvious situation that must arise all the time for people: struct Foundation { var columns : [Column] = [Column(), Column()] } struct Column : CustomStringConvertible { var cards = [Card]() var description : String { return String(describing:self.cards) } } struct Card {} var f = Foundation() for var c in f.columns { c.cards.append(Card()) } That code is legal but of course it has no effect on f , because var c is still a copy — the actual columns of f are unaffected. I am not having any difficulties understanding why that happens. My question is what people generally do about it.

Best form for constructors? Pass by value or reference?

荒凉一梦 提交于 2019-12-03 02:31:25
问题 I'm wondering the best form for my constructors. Here is some sample code: class Y { ... } class X { public: X(const Y& y) : m_y(y) {} // (a) X(Y y) : m_y(y) {} // (b) X(Y&& y) : m_y(std::forward<Y>(y)) {} // (c) Y m_y; } Y f() { return ... } int main() { Y y = f(); X x1(y); // (1) X x2(f()); // (2) } From what I understand, this is the best the compiler can do in each situation. (1a) y is copied into x1.m_y (1 copy) (1b) y is copied into the argument of the constructor of X, and then copied

Are variables/objects passed by value and why can't I change object's property with variable in javascript? [duplicate]

喜欢而已 提交于 2019-12-02 18:21:14
问题 This question already has answers here : Is JavaScript a pass-by-reference or pass-by-value language? (30 answers) Closed 3 years ago . Suppose I have an object as: var obj = { len: 4, bred: 5 } Now suppose I assign this object to a variable x as var x = obj; . As far as I understand it, it creates a copy of obj and assign that copy to x  — that is pass by value. Now If I change a property of x then it changes that property of the obj object too. E.g. x.len = 99 Then both obj.len and x.len

Best form for constructors? Pass by value or reference?

丶灬走出姿态 提交于 2019-12-02 16:23:44
I'm wondering the best form for my constructors. Here is some sample code: class Y { ... } class X { public: X(const Y& y) : m_y(y) {} // (a) X(Y y) : m_y(y) {} // (b) X(Y&& y) : m_y(std::forward<Y>(y)) {} // (c) Y m_y; } Y f() { return ... } int main() { Y y = f(); X x1(y); // (1) X x2(f()); // (2) } From what I understand, this is the best the compiler can do in each situation. (1a) y is copied into x1.m_y (1 copy) (1b) y is copied into the argument of the constructor of X, and then copied into x1.m_y (2 copies) (1c) y is moved into x1.m_y (1 move) (2a) result of f() is copied into x2.m_y (1

Pass by value faster than pass by reference

ε祈祈猫儿з 提交于 2019-12-02 14:22:31
I made a simple program in c++ to compare performance between two approaches - pass by value and pass by reference. Actually pass by value performed better than pass by reference. The conclusion should be that passing by value require fewer clock-cycles (instructions) I would be really glad if someone could explain in detail why pass by value require fewer clock-cycles. #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; void function(int *ptr); void function2(int val); int main() { int nmbr = 5; clock_t start, stop; start = clock(); for (long i = 0; i < 1000000000;

Why isn't `src` changing when I change it from the function?

一笑奈何 提交于 2019-12-02 11:45:27
问题 My program: #include<stdio.h> #include<conio.h> char* str(char*src);\\declared as function global main() { char src[40]="hello"; clrscr(); puts(src); str(src);\\ function called puts(src);\\does not put changed string? getch(); return 0; } char* str( char *src) { src="readers";\\changing my source string<--is it changes??? return src;\\returning source sting } output: hello hello Why is src not becoming "readers" ? 回答1: In your code, src="readers"; is wrong. You have to try like strcpy(src,

Are variables/objects passed by value and why can't I change object's property with variable in javascript? [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-02 10:04:07
This question already has an answer here: Is JavaScript a pass-by-reference or pass-by-value language? 29 answers Suppose I have an object as: var obj = { len: 4, bred: 5 } Now suppose I assign this object to a variable x as var x = obj; . As far as I understand it, it creates a copy of obj and assign that copy to x  — that is pass by value. Now If I change a property of x then it changes that property of the obj object too. E.g. x.len = 99 Then both obj.len and x.len become 99 . On the other hand consider this scenario: var r = 2, s = 3, t = 4; s = r; s = 88; Now r is passed by value to s

Why isn't `src` changing when I change it from the function?

喜欢而已 提交于 2019-12-02 04:02:58
My program: #include<stdio.h> #include<conio.h> char* str(char*src);\\declared as function global main() { char src[40]="hello"; clrscr(); puts(src); str(src);\\ function called puts(src);\\does not put changed string? getch(); return 0; } char* str( char *src) { src="readers";\\changing my source string<--is it changes??? return src;\\returning source sting } output: hello hello Why is src not becoming "readers" ? In your code, src="readers"; is wrong. You have to try like strcpy(src, "readers"); The reason is, C uses pass by value while passing function parameters. So, src here is passed by

ByRef seems to receive the value and not the reference in VBA 6.0

江枫思渺然 提交于 2019-12-01 21:29:25
My little sample code Function AddNr(ByRef x As Integer) As Integer x = x + 2 AddNr = x End Function Sub test() Dim ana As Integer ana = 1 AddNr (ana) MsgBox ana End Sub should output 3 but outputs 1. To be more specific the ana variable is not modified after the call to the AddNr function. My environment is Microsoft Visual Basic 6.5 inside Excel 2007. That should be: AddNr ana That is, no brackets. From Microsoft Help: Remarks You are not required to use the Call keyword when calling a procedure. However, if you use the Call keyword to call a procedure that requires arguments, argumentlist

Why is copy constructor not called

强颜欢笑 提交于 2019-12-01 20:44:30
问题 Here is a simple class header file and a main program. In the main program, I thought that the copy constructor is called in exactly three situations: initialization(explicit copy), pass by value for function arguments, and return by value for functions. However it seems like it is not being called for one of them, I think either (3) or (4) as numbered in the comments. For which numbers (1) - (4) does it get called? Thanks. X.h: #include <iostream> class X { public: X() {std::cout << "default