pass-by-value

Why are objects automatically passed by reference?

左心房为你撑大大i 提交于 2019-11-30 03:15:09
问题 I have a general question about deep- and shallow-copy in the context of the pass-by-reference- and pass-by-value-concept of C#: In C# it is a requirement to explicitly create methods that accept pointers/references to be able to pass such to the method. However, at least objects passed as parameters to methods/constructors are behaving differently from the rest. It seems they are always passed by reference if no extra cloning is done as described here: http://zetcode.com/lang/csharp/oopii/.

Confused by single pointer and double pointer arguments in function calls

孤街醉人 提交于 2019-11-30 02:31:53
I'm trying to get a deeper understanding on pointer arguments in functions for C. I've written a test program to try to see the difference between passing a single pointer vs a double pointer to a function and then modifying it. I have a program that has two functions. The first function modifyMe1 takes a single pointer as an argument and changes the a property to 7. The second function modifyMe2 takes a double pointer as an argument and changes the a property to 7. I expected that the first function modifyMe1 , would be "pass-by-value" that is if I passed in my struct pointer, C would create

Isn't “const” redundant when passing by value? [duplicate]

二次信任 提交于 2019-11-29 21:58:47
This question already has an answer here: Use of 'const' for function parameters 30 answers I was reading my C++ book (Deitel) when I came across a function to calculate the volume of a cube. The code is the following: double cube (const double side){ return side * side * side; } The explanation for using the "const" qualifier was this one: "The const qualified should be used to enforce the principle of least privilege, telling the compiler that the function does not modify variable side". My question : isn't the use of "const" redundant/unnecessary here since the variable is being passed by

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

久未见 提交于 2019-11-29 18:44:58
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<<"pp(A* a)"<<std::endl;} void pc(const A& a){std::cout<<"pc(const A& a)"<<std::endl;} int main() { std:

C++ Pass by Reference Program

有些话、适合烂在心里 提交于 2019-11-29 12:57:37
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 Any swapping performed if you pass by value are only affected or seen within the function they are passed into and not the calling code. In addition, once you return back to main you will see

Modify an array passed as a method-parameter

微笑、不失礼 提交于 2019-11-29 07:16:41
Suppose I have an int-array and I want to modify it. I know that I cannot assign a new array to array passed as parameter: public static void main(String[] args) { int[] temp_array = {1}; method(temp_array); System.out.println(temp_array[0]); // prints 1 } public static void method(int[] n) { n = new int[]{2}; } while I can modify it: public static void main(String[] args) { int[] temp_array = {1}; method(temp_array); System.out.println(temp_array[0]); // prints 2 } public static void method(int[] n) { n[0] = 2; } Then, I tried to assign an arbitrary array to the array passed as parameter

Why is a volatile local variable optimised differently from a volatile argument, and why does the optimiser generate a no-op loop from the latter?

孤街醉人 提交于 2019-11-29 06:53:05
Background This was inspired by this question/answer and ensuing discussion in the comments: Is the definition of “volatile” this volatile, or is GCC having some standard compliancy problems? . Based on others' and my interpretation of what should happening, as discussed in comments, I've submitted it to GCC Bugzilla: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71793 Other relevant responses are still welcome. Also, that thread has since given rise to this question: Does accessing a declared non-volatile object through a volatile reference/pointer confer volatile rules upon said accesses?

Immutable and pass by value

ぐ巨炮叔叔 提交于 2019-11-29 03:04:38
问题 I have the following code which has a mutable Person class, String and a method to modify the instances of String and Person class Person{ int a = 8; public int getA() { return a; } public void setA(int a) { this.a = a; } @Override public String toString() { return "Person [a=" + a + "]"; } } -- public class TestMutable { public static void main(String[] args) { Person p = new Person(); p.setA(34); String s = "bar"; modifyObject(s, p); //Call to modify objects System.out.println(s); System

C++ pass list as a parameter to a function

柔情痞子 提交于 2019-11-29 02:46:15
问题 I'm trying to build a very simple address book. I created a Contact class and the address book is a simple list. I'm trying to build a function to allow the user to add contacts to the address book. If I take my code outside of the function, it works OK. However, if I put it in, it doesn't work. I believe it's a passing by reference vs passing by value problem which I'm not treating as I should. This is the code for the function: void add_contact(list<Contact> address_book) { //the local

Rule of thumb for when passing by value is faster than passing by const reference?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 01:00:44
Suppose I have a function that takes an argument of type T . It does not mutate it, so I have the choice of passing it by const reference const T& or by value T : void foo(T t){ ... } void foo(const T& t){ ... } Is there a rule of thumb of how big T should become before passing by const reference becomes cheaper than passing by value? E.g., suppose I know that sizeof(T) == 24 . Should I use const reference or value? I assume that the copy constructor of T is trivial. Otherwise, the answer to the question depends on the complexity of the copy constructor, of course. I have already looked for