pass-by-value

Is Objective-C pass-by-value or pass-by-reference?

烈酒焚心 提交于 2019-11-28 21:38:28
Since we always use pointers to define variables, I was wondering if Objective-C is "pass by value", since like Java, the actual value would be passed by using its reference. However, since it seems to be built up on top of C, would it have all the functionality of C? C does not support pass-by-reference and Objective-C, being a strict superset of C doesn't either. In C (and Objective-C) you can simulate pass-by-reference by passing a pointer, but it's important to remember that you're still technically passing a value, which happens to be a the value of a pointer. So, in Objective-C (and C,

How are arrays passed?

情到浓时终转凉″ 提交于 2019-11-28 21:16:05
Are arrays passed by default by ref or value? Thanks. They are passed as pointers. This means that all information about the array size is lost. You would be much better advised to use std::vectors, which can be passed by value or by reference, as you choose, and which therefore retain all their information. Here's an example of passing an array to a function. Note we have to specify the number of elements specifically, as sizeof(p) would give the size of the pointer. int add( int * p, int n ) { int total = 0; for ( int i = 0; i < n; i++ ) { total += p[i]; } return total; } int main() { int a[

Variable changed in function not seen by caller?

柔情痞子 提交于 2019-11-28 14:12:31
Yes, I know it sounds silly but i have no idea what i'm doing wrong! The function is part of a poker game, in which there are 10 functions, each which checks for a specific poker hand. If activated, the function prints the line "Player 1 has a full house!" or whatever the hand might be. But, i also need to increment the value of p1, in which p1 is a global variable the holds the total score for p1. Printing the line works perfectly, but when i want to assign, for example, a value of 10 to p1, it simply doesn't assign. In the example below, the printfs work perfectly when they should, but the

Pass by value or reference, to a C++ constructor that needs to store a copy?

吃可爱长大的小学妹 提交于 2019-11-28 13:48:45
Should a C++ (implicit or explicit) value constructor accept its parameter(s) by value or reference-to-const, when it needs to store a copy of the argument(s) in its object either way? Here is the shortest example I can think of: struct foo { bar _b; foo(bar [const&] b) // pass by value or reference-to-const? : _b(b) { } }; The idea here is that I want to minimize the calls to bar's copy constructor when a foo object is created, in any of the various ways in which a foo object might get created. Please note that I do know a little bit about copy elision and (Named) Return Value Optimization,

What operators do I have to overload to see all operations when passing an object to a function?

佐手、 提交于 2019-11-28 12:28:24
问题 I would like to write a piece of code that shows all copy/assignment/delete etc. operations that are done on the object when passing it to a function. I wrote this: #include <iostream> class A { public: A(){std::cout<<"A()"<<std::endl;} void operator=(const A& a ){std::cout<<"A=A"<<std::endl;} A(const A& a){std::cout<<"A(A)"<<std::endl;} ~A(){std::cout<<"~A"<<std::endl;} }; void pv(A a){std::cout<<"pv(A a)"<<std::endl;} void pr(A& a){std::cout<<"pr(A& a)"<<std::endl;} void pp(A* a){std::cout<

Is There a Reason Standard Algorithms Take Lambdas by Value? [duplicate]

ぃ、小莉子 提交于 2019-11-28 12:09:23
This question already has an answer here: Why the sequence-operation algorithms predicates are passed by copy? 3 answers So I asked a question here: Lambda Works on Latest Visual Studio, but Doesn't Work Elsewhere to which I got the response, that my code was implementation defined since the standard's 25.1 [algorithms.general] 10 says: Unless otherwise specified, algorithms that take function objects as arguments are permitted to copy those function objects freely. Programmers for whom object identity is important should consider using a wrapper class that points to a noncopied implementation

Call by reference, value, and name

爷,独闯天下 提交于 2019-11-28 07:27:48
问题 I'm trying to understand the conceptual difference between call by reference, value, and name. So I have the following pseudocode: foo(a, b, c) { b =b++; a = a++; c = a + b*10 } X=1; Y=2; Z=3; foo(X, Y+2, Z); What's X, Y, and Z after the foo call if a, b, and c are all call by reference? if a, b, and c are call-by-value/result? if a, b, and c are call-by-name? Another scenario: X=1; Y=2; Z=3; foo(X, Y+2, X); I'm trying to get a head start on studying for an upcoming final and this seemed like

C++ Pass by Reference Program

好久不见. 提交于 2019-11-28 07:04:51
问题 IBM explains C++ pass by reference in the example below (source included). If I changed void swapnum... to void swapnum(int i, int j) , would it become pass by value? // pass by reference example // author - ibm #include <stdio.h> void swapnum(int &i, int &j) { int temp = i; i = j; j = temp; } int main(void) { int a = 10; int b = 20; swapnum(a, b); printf("A is %d and B is %d\n", a, b); return 0; } Source 回答1: Any swapping performed if you pass by value are only affected or seen within the

why function objects should be pass-by-value

北慕城南 提交于 2019-11-28 06:25:43
I have just read the classic book "Effective C++, 3rd Edition", and in item 20 the author concludes that built-in types, STL iterators and function object types are more appropriate for pass-by-value . I could well understand the reason for built-in and iterators types, but why should the function object be pass-by-value , as we know it is class-type anyway? In a typical case, a function object will have little or (more often) no persistent state. In such a case, passing by value may no require actually passing anything at all -- the "value" that's passed is basically little or nothing more

How to access variable by id? [duplicate]

岁酱吖の 提交于 2019-11-28 04:16:01
问题 Possible Duplicate: Get object by id()? >>> var = 'I need to be accessed by id!' >>> address = id(var) >>> print(address) 33003240 Is there any way to use address of var in memory, provided by id() , for accessing value of var ? UPD: I also want to say, that if this cannot be done in standard Python, it also would be interesting, if this somehow could be implemented via hacking C++ internals of Python. UPD2: Also would be interesting to know, how to change value of var . 回答1: A solution, only